from __future__ import annotations

from pathlib import Path
from typing import Annotated

from typer import Argument, Option, Typer

adapters_app = Typer(no_args_is_help=True)


@adapters_app.command()
def init(
    adapter_id: Annotated[
        str | None,
        Argument(
            help=(
                "Adapter ID (lowercase, no spaces). Leave empty to derive from --name."
            ),
        ),
    ] = None,
    adapters_dir: Annotated[
        Path,
        Option(
            "--adapters-dir",
            help="Directory in which to create the adapter folder.",
        ),
    ] = Path("adapters"),
    name: Annotated[
        str | None,
        Option(
            "--name",
            "-n",
            help="Vanilla benchmark name (e.g., SWE-bench, MLEBench)",
        ),
    ] = None,
    class_name: Annotated[
        str | None,
        Option(
            "--class-name",
            help="Override adapter class name (defaults from --name)",
        ),
    ] = None,
    description: Annotated[
        str | None,
        Option("--description", "-d", help="One-line adapter description for README"),
    ] = None,
    source_url: Annotated[
        str | None,
        Option("--source-url", help="Source repository or paper URL"),
    ] = None,
    license_name: Annotated[
        str | None,
        Option("--license", help="Dataset/benchmark license (for README)"),
    ] = None,
) -> None:
    """Launch the rich interactive wizard to initialize a new adapter template."""
    from harbor.cli.adapter_wizard import AdapterWizard

    wizard = AdapterWizard(
        adapters_dir,
        name=name,
        adapter_id=adapter_id,
        class_name=class_name,
        description=description,
        source_url=source_url,
        license_name=license_name,
    )
    wizard.run()


@adapters_app.command()
def review(
    path: Annotated[
        Path,
        Option(
            "--path",
            "-p",
            help="Path to the adapter directory to review.",
        ),
    ],
    agent: Annotated[
        str,
        Option(
            "--agent",
            "-a",
            help="Agent for AI review: 'claude' or 'codex'.",
        ),
    ] = "claude",
    skip_ai: Annotated[
        bool,
        Option(
            "--skip-ai",
            help="Skip AI review, run only structural validation.",
        ),
    ] = False,
    model: Annotated[
        str | None,
        Option(
            "--model",
            "-m",
            help="Model for AI review. If not set, uses the agent's default model.",
        ),
    ] = None,
    original_fork_repo: Annotated[
        Path | None,
        Option(
            "--original-fork-repo",
            help="Path to the original benchmark fork repo for cross-review.",
        ),
    ] = None,
    output: Annotated[
        Path,
        Option(
            "--output",
            "-o",
            help="Save review to a Markdown file.",
        ),
    ] = Path("adapter-review-report.md"),
) -> None:
    """Review an adapter for structural compliance and code quality.

    Runs in up to three parts:

    1. Structural validation — checks required files, JSON schemas, canary strings,
       PR links, README sections, and cross-file consistency. No AI needed.

    2. AI review — uses an AI agent to perform a semantic code review.
       Supports --agent claude (local Claude Code) or --agent codex (local Codex CLI).
       Both run locally and read files from the adapter directory.
       Uses the agent's default model unless --model is specified.
       Use --skip-ai to run only structural validation.

    3. Original fork review (optional) — if --original-fork-repo is provided,
       reviews the forked benchmark repo for consistency with the adapter:
       fork existence/structure, README instructions, modifications, bug fixes,
       and alignment with Harbor parity settings.

    Results are saved to a Markdown report (default: adapter-review-report.md).
    """
    adapter_dir = Path(path)
    if not adapter_dir.is_dir():
        # Try resolving as adapter name under adapters/
        candidate = Path("adapters") / path
        if candidate.is_dir():
            adapter_dir = candidate
        else:
            print(f"Error: adapter directory not found: {path}")
            raise SystemExit(1)

    from harbor.cli.adapter_review import (
        run_ai_review,
        run_structural_validation,
        save_review,
    )

    print(f"=== Adapter Review: {adapter_dir.name} ===")

    # Part 1: Structural validation
    print("\n--- Part 1: Structural Validation ---")
    passed, structural_md = run_structural_validation(adapter_dir)

    # Part 2: AI review (includes fork review if --original-fork-repo provided)
    ai_md = ""
    fork_dir: Path | None = None
    if original_fork_repo is not None:
        fork_dir = Path(original_fork_repo)
        if not fork_dir.is_dir():
            print(f"Error: fork repo directory not found: {original_fork_repo}")
            raise SystemExit(1)

    if not skip_ai:
        print("\n--- Part 2: AI Review ---")
        ai_md = run_ai_review(adapter_dir, agent=agent, model=model, fork_dir=fork_dir)
    else:
        print("\n--- Part 2: AI Review (skipped) ---")

    # Save combined report
    save_review(
        adapter_dir.name, structural_md, ai_md, output, agent=agent, model=model
    )

    if not passed:
        raise SystemExit(1)
