from pathlib import Path
import subprocess
import sys
from typing import Optional

import typer

app = typer.Typer(add_completion=False, help="Mini Agent Harness CLI")


@app.command()
def init(agent_name: Optional[str] = typer.Argument("quickstart", help="Name of the example agent to generate")):
    """Generate an example agent YAML stub under the ./agents folder."""
    agents_dir = Path("agents")
    agents_dir.mkdir(exist_ok=True)

    target_path = agents_dir / f"{agent_name}.yaml"
    if target_path.exists():
        typer.echo(f"⚠️  {target_path} already exists. Skipping generation.")
        raise typer.Exit()

    yaml_stub = (
        "name: Quickstart Agent\n"
        "description: A minimal agent spec example generated by mini-agent init.\n"
        "tools: []\n"
    )
    target_path.write_text(yaml_stub)
    typer.echo(f"✅ Generated {target_path}")


@app.command()
def serve(model: str = typer.Option("gpt-3.5-turbo", "--model", help="Model name or path to serve with")):
    """Serve the agent via FastAPI (placeholder implementation)."""
    typer.echo(
        "🚧 The serve command is not implemented yet. Run `mini-agent init` and explore tests in the meantime."
    )


@app.command()
def test(pytest_args: Optional[str] = typer.Argument(None, help="Additional arguments forwarded to pytest")):
    """Run the project's pytest suite with the built-in harness."""
    cmd = [sys.executable, "-m", "pytest"]
    if pytest_args:
        cmd.extend(pytest_args.split())
    raise SystemExit(subprocess.call(cmd))


if __name__ == "__main__":
    app() 