"""Init subcommand for agr - create new resources and repos."""

from pathlib import Path
from typing import Annotated

import typer
from rich.console import Console

from agr.github import check_gh_cli, create_github_repo, get_github_username, repo_exists
from agr.scaffold import (
    create_agent_resources_repo,
    init_git,
    scaffold_repo,
    write_gitignore,
    write_readme,
    write_starter_agent,
    write_starter_command,
    write_starter_skill,
)

console = Console()

app = typer.Typer(
    help="Create new agent resources or repositories.",
    no_args_is_help=True,
)


@app.command("repo")
def init_repo(
    name: Annotated[
        str,
        typer.Argument(
            help="Name of the repository to create (default: agent-resources)",
            metavar="NAME",
        ),
    ] = "agent-resources",
    path: Annotated[
        Path | None,
        typer.Option(
            "--path",
            "-p",
            help="Custom path for the repository (default: ./<name>)",
        ),
    ] = None,
    github: Annotated[
        bool,
        typer.Option(
            "--github",
            "-g",
            help="Create a GitHub repository and push",
        ),
    ] = False,
) -> None:
    """Create a new agent-resources repository with starter content.

    Creates a directory structure with example skill, command, and agent files.

    Examples:
      agr init repo                          # Creates ./agent-resources/
      agr init repo my-resources             # Creates ./my-resources/
      agr init repo agent-resources --github # Creates and pushes to GitHub
    """
    # Determine target path
    target_path = path or Path.cwd() / name

    if target_path.exists():
        console.print(f"[red]Error: Directory already exists: {target_path}[/red]")
        raise typer.Exit(1)

    # Check GitHub CLI if --github flag is set
    if github:
        if not check_gh_cli():
            console.print(
                "[red]Error: GitHub CLI (gh) is not installed or not authenticated.[/red]\n"
                "Install it from https://cli.github.com/ and run 'gh auth login'"
            )
            raise typer.Exit(1)

        if repo_exists(name):
            console.print(f"[red]Error: GitHub repository '{name}' already exists.[/red]")
            raise typer.Exit(1)

    # Get GitHub username for README
    username = get_github_username() or "<username>"

    # Create the repository
    console.print(f"Creating agent-resources repository at {target_path}...")
    create_agent_resources_repo(target_path, username)

    # Initialize git
    if init_git(target_path):
        console.print("[green]Initialized git repository[/green]")
    else:
        console.print("[yellow]Warning: Could not initialize git repository[/yellow]")

    # Create GitHub repo if requested
    if github:
        console.print("Creating GitHub repository...")
        repo_url = create_github_repo(target_path, name)
        if repo_url:
            console.print(f"[green]Created and pushed to {repo_url}[/green]")
            console.print(f"\nOthers can now install your resources:")
            console.print(f"  agr add skill {username}/hello-world")
            console.print(f"  agr add command {username}/hello")
            console.print(f"  agr add agent {username}/hello-agent")
        else:
            console.print("[yellow]Warning: Could not create GitHub repository[/yellow]")
    else:
        console.print(f"\n[green]Created agent-resources repository at {target_path}[/green]")
        console.print("\nNext steps:")
        console.print(f"  cd {target_path}")
        console.print("  git remote add origin <your-repo-url>")
        console.print("  git push -u origin main")


@app.command("skill")
def init_skill(
    name: Annotated[
        str,
        typer.Argument(
            help="Name of the skill to create",
            metavar="NAME",
        ),
    ],
    path: Annotated[
        Path | None,
        typer.Option(
            "--path",
            "-p",
            help="Custom path (default: ./.claude/skills/<name>/)",
        ),
    ] = None,
) -> None:
    """Create a new skill scaffold.

    Examples:
      agr init skill my-skill
      agr init skill code-reviewer --path ./custom/path/
    """
    target_path = path or (Path.cwd() / ".claude" / "skills" / name)
    skill_file = target_path / "SKILL.md"

    if skill_file.exists():
        console.print(f"[red]Error: Skill already exists at {skill_file}[/red]")
        raise typer.Exit(1)

    target_path.mkdir(parents=True, exist_ok=True)

    skill_content = f"""\
---
name: {name}
description: Description of what this skill does
---

# {name.replace('-', ' ').title()} Skill

Describe what this skill does and when Claude should apply it.

## When to Use

Describe the situations when this skill should be applied.

## Instructions

Provide specific instructions for Claude to follow.
"""
    skill_file.write_text(skill_content)
    console.print(f"[green]Created skill at {skill_file}[/green]")


@app.command("command")
def init_command(
    name: Annotated[
        str,
        typer.Argument(
            help="Name of the command to create (without leading slash)",
            metavar="NAME",
        ),
    ],
    path: Annotated[
        Path | None,
        typer.Option(
            "--path",
            "-p",
            help="Custom path (default: ./.claude/commands/<name>.md)",
        ),
    ] = None,
) -> None:
    """Create a new slash command scaffold.

    Examples:
      agr init command my-command
      agr init command deploy --path ./custom/path/
    """
    target_path = path or (Path.cwd() / ".claude" / "commands")
    command_file = target_path / f"{name}.md"

    if command_file.exists():
        console.print(f"[red]Error: Command already exists at {command_file}[/red]")
        raise typer.Exit(1)

    target_path.mkdir(parents=True, exist_ok=True)

    command_content = f"""\
---
description: Description of /{name} command
---

When the user runs /{name}, do the following:

1. First step
2. Second step
3. Third step

Provide clear, actionable instructions for what Claude should do.
"""
    command_file.write_text(command_content)
    console.print(f"[green]Created command at {command_file}[/green]")


@app.command("agent")
def init_agent(
    name: Annotated[
        str,
        typer.Argument(
            help="Name of the agent to create",
            metavar="NAME",
        ),
    ],
    path: Annotated[
        Path | None,
        typer.Option(
            "--path",
            "-p",
            help="Custom path (default: ./.claude/agents/<name>.md)",
        ),
    ] = None,
) -> None:
    """Create a new sub-agent scaffold.

    Examples:
      agr init agent my-agent
      agr init agent test-writer --path ./custom/path/
    """
    target_path = path or (Path.cwd() / ".claude" / "agents")
    agent_file = target_path / f"{name}.md"

    if agent_file.exists():
        console.print(f"[red]Error: Agent already exists at {agent_file}[/red]")
        raise typer.Exit(1)

    target_path.mkdir(parents=True, exist_ok=True)

    agent_content = f"""\
---
description: Description of the {name} sub-agent
---

You are a specialized sub-agent for {name.replace('-', ' ')}.

## Purpose

Describe the specific purpose and capabilities of this agent.

## Instructions

When invoked, you should:

1. First action
2. Second action
3. Third action

## Constraints

- Constraint 1
- Constraint 2
"""
    agent_file.write_text(agent_content)
    console.print(f"[green]Created agent at {agent_file}[/green]")
