"""CLI registration for dopetask tp exec command."""

from __future__ import annotations

import json
from pathlib import Path

import typer

from dopetask.core.tp_parser import TPNormalizer, TPParser
from dopetask.ops.tp_exec.engine import execute_task_packet
from dopetask.ops.tp_tmux.tmux_manager import TmuxManager


def register(tp_app: typer.Typer) -> None:
    """Attach tp exec command to the tp group."""

    @tp_app.command("exec")
    def tp_exec(
        tp_file: Path = typer.Argument(
            ...,
            exists=True,
            file_okay=True,
            dir_okay=False,
            help="Generic Task Packet JSON file.",
        ),
        agent: str = typer.Option(
            "gemini",
            "--agent",
            help="Agent profile: gemini, codex, or vibe.",
        ),
        dry_run: bool = typer.Option(
            False,
            "--dry-run",
            help="Compile and show prompts without executing.",
        ),
        use_tmux: bool = typer.Option(
            False,
            "--tmux",
            help="Run execution inside an isolated tmux session.",
        ),
    ) -> None:
        """Execute a Task Packet using a specific agent profile."""
        try:
            resolved_tp_file = tp_file.resolve()
            tp = TPParser.parse_file(resolved_tp_file)

            if use_tmux:
                manager = TmuxManager()
                session_name = f"tp-{tp.id.lower()}"
                # Construct command to run itself without --tmux
                cmd = f"dopetask tp exec {resolved_tp_file} --agent {agent}"
                if manager.start_session(session_name, Path.cwd(), cmd):
                    typer.echo(f"Spawned execution in tmux session: {session_name}")
                    typer.echo(f"Run 'dopetask tmux attach {tp.id}' to monitor.")
                    raise typer.Exit(0)
                else:
                    typer.echo(f"Failed to start tmux session '{session_name}'. It might already exist.", err=True)
                    raise typer.Exit(1)

            if dry_run:
                typer.echo(f"--- Compiled Profile: {agent} ---")
                typer.echo(json.dumps(TPNormalizer.compile(tp, agent), indent=2))
                raise typer.Exit(0)

            bundle_path = execute_task_packet(resolved_tp_file, agent=agent)
            typer.echo(f"Success! Canonical Proof Bundle written to: {bundle_path}")
            typer.echo("Audit archive created in same directory.")

        except typer.Exit:
            raise
        except ValueError as exc:
            typer.echo(f"Compilation Failed: {exc}", err=True)
            raise typer.Exit(1) from exc
        except Exception as exc:
            typer.echo(f"Error: {exc}", err=True)
            raise typer.Exit(1) from exc
