from __future__ import annotations

import typer
import rich

console = rich.get_console()
view_app = typer.Typer(
    name="view",
    help="View results in the results folder",
    rich_markup_mode="rich",
    no_args_is_help=True,
)


@view_app.command(name="validate-model", no_args_is_help=True)
def cli_view_validate_model(
    model: str = typer.Option(
        ...,
        "--model",
        "-m",
        help="Name of the model organism to view validate-model results for",
    ),
) -> None:
    """
    Views all logs from model --model, all dates
    """
    from pathlib import Path
    import subprocess
    
    logs_path = Path("results/validate_model") / model
    if not logs_path.exists():
        console.print(f"No logs found for model {model}")
        raise typer.Exit(1)
    subprocess.run(["inspect", "view", "--log-dir", logs_path])


@view_app.command(name="run-detector", no_args_is_help=True)
def detectoriew_run_detector(
    detector: str = typer.Option(
        ...,
        "--detector",
        "-d",
        help="Name of the detector to view run-detector results for",
    ),
) -> None:
    """
    Views all logs from detector --detector, all dates
    """
    from pathlib import Path
    import subprocess
    
    logs_path = Path("results/run_detector") / detector
    if not logs_path.exists():
        console.print(f"No logs found for detector {detector}")
        raise typer.Exit(1)
    subprocess.run(["inspect", "view", "--log-dir", logs_path])


@view_app.command(name="latest")
def cli_view_experiment_latest(
    port: int = typer.Option(
        7575,
        "--port",
        "-p"
    )
) -> None:
    """
    View the latest experiment run, either validate-model or run-detector
    """
    from pathlib import Path
    from rich import get_console
    console = get_console()
    import subprocess
    
    base_dirs = [Path("results/validate_model"), Path("results/run_detector")]

    latest_folder: Path | None = None
    latest_mtime = 0.0

    for base_dir in base_dirs:
        if not base_dir.exists():
            continue
        for subfolder in base_dir.iterdir():
            if subfolder.is_dir():
                mtime = subfolder.stat().st_mtime
                if mtime > latest_mtime:
                    latest_mtime = mtime
                    latest_folder = subfolder

    if latest_folder is None:
        console.print("No experiment folders found.")
        return

    logs_path = latest_folder
    console.print(f"Viewing logs at: {logs_path}")

    subprocess.run(["inspect", "view", "--log-dir",
                   str(logs_path), "--port", str(port)])
