# Copyright (C) 2026 GoodData Corporation

"""Argument parser construction for the AIDA CLI."""

from __future__ import annotations

import argparse

from aida_mcp.server.cli_handlers_git_alias import (
    alias_install_cmd,
    alias_status_cmd,
    alias_uninstall_cmd,
    commit_cmd,
)
from aida_mcp.server.cli_handlers_lifecycle import doctor_cmd, init_cmd, install_mcp, migrate_cmd


def build_arg_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="aida-mcp", add_help=True)
    sub = parser.add_subparsers(dest="command")

    init = sub.add_parser(
        "init",
        help=(
            "Initialize this repo for AIDA (create .aida skeleton + repo-local MCP stubs for Cursor/Claude; "
            "JetBrains/Junie require --global install-mcp)."
        ),
    )
    init.add_argument("--workspace-root", help="Target workspace root (default: cwd).")
    init.add_argument("--force", action="store_true", help="Overwrite files where safe (stubs/config).")
    init.set_defaults(_handler=init_cmd)

    doctor = sub.add_parser("doctor", help="Check whether this repo is ready for AIDA.")
    doctor.add_argument("--workspace-root", help="Target workspace root (default: cwd).")
    doctor.set_defaults(_handler=doctor_cmd)

    migrate = sub.add_parser("migrate", help="Migrate legacy repo/client setup to AIDA.")
    migrate.add_argument("--workspace-root", help="Target workspace root (default: cwd).")
    migrate.add_argument(
        "--client-only", action="store_true", help="Only migrate client instruction files (no config dir rename)."
    )
    migrate.add_argument("--dry-run", action="store_true", help="Report actions without writing/removing files.")
    migrate.add_argument("--force", action="store_true", help="Overwrite/replace when conflicts exist.")
    migrate.set_defaults(_handler=migrate_cmd)

    commit = sub.add_parser(
        "commit",
        help="Create git commit with enforced footer policy (risk required, JIRA optional).",
    )
    commit.add_argument("--workspace-root", help="Target workspace root (default: cwd).")
    commit.add_argument("--all", action="store_true", help="Stage all changes (`git add -A`) before committing.")
    commit.add_argument("--dry-run", action="store_true", help="Print composed message without creating a commit.")
    commit.add_argument("--type", default="chore", help="Commit type prefix (default: chore).")
    commit.add_argument("--scope", help="Optional commit scope, rendered as type(scope): ...")
    commit.add_argument("--title", required=True, help="Commit title text (subject line).")
    body_group = commit.add_mutually_exclusive_group()
    body_group.add_argument("--body", help="Commit body text (single argument; may include newlines).")
    body_group.add_argument("--body-file", help="Path to file containing commit body text.")
    body_group.add_argument("--body-stdin", action="store_true", help="Read commit body from stdin.")
    body_group.add_argument(
        "--body-line", action="append", help="Commit body line (repeatable). Lines are joined with newline."
    )
    commit.add_argument("--risk", required=True, help="Required risk footer value (see .aida/git_policy.yaml).")
    commit.add_argument("--jira", action="append", help="Optional JIRA ID (repeat flag for multiple values).")
    commit.add_argument(
        "--co-authored-by", action="append", help="Optional co-author value (`Name <email>`). Repeatable."
    )
    ai_share_group = commit.add_mutually_exclusive_group()
    ai_share_group.add_argument(
        "--ai-share",
        action="append",
        help="Optional AI contribution entry as actor=percent (repeatable, total must equal 100).",
    )
    ai_share_group.add_argument(
        "--ai-share-auto", help="Set deterministic AI share as <actor>=100 (for example: cursor, claude). Policy-gated."
    )
    commit.set_defaults(_handler=commit_cmd)

    install = sub.add_parser("install-mcp", help="Install MCP config for supported MCP clients")
    install.add_argument(
        "--target",
        action="append",
        choices=("cursor", "claude", "codex", "jetbrains", "junie", "all"),
        help="Target config(s) to install. Repeatable. Use 'all' for all targets.",
    )
    install.add_argument(
        "--global",
        dest="global_config",
        action="store_true",
        help=(
            "Write global ($HOME) client configs. Required for jetbrains/junie "
            "(~/.ai/mcp/mcp.json and ~/.junie/mcp/mcp.json)."
        ),
    )
    install.add_argument(
        "--workspace-root", help="Optional: pin `AIDA_WORKSPACE_ROOT` in the installed config (single-repo mode)."
    )
    install.add_argument(
        "--config-root", help="Optional: pin `AIDA_CONFIG_ROOT` in the installed config (single-repo mode)."
    )
    install.set_defaults(_handler=install_mcp)

    alias = sub.add_parser("alias", help="Manage optional 'aida' CLI alias (wrapper next to aida-mcp).")
    alias_sub = alias.add_subparsers(dest="alias_command")

    alias_status = alias_sub.add_parser("status", help="Show alias status and PATH resolution")
    alias_status.add_argument("--name", default="aida", help="Alias command name (default: aida)")
    alias_status.set_defaults(_handler=alias_status_cmd)

    alias_install = alias_sub.add_parser("install", help="Install alias wrapper (fails if name exists on PATH)")
    alias_install.add_argument("--name", default="aida", help="Alias command name (default: aida)")
    alias_install.add_argument(
        "--force",
        action="store_true",
        help="Install even if the alias name already exists on PATH (PATH precedence still applies).",
    )
    alias_install.set_defaults(_handler=alias_install_cmd)

    alias_uninstall = alias_sub.add_parser("uninstall", help="Remove alias wrapper installed next to aida-mcp")
    alias_uninstall.add_argument("--name", default="aida", help="Alias command name (default: aida)")
    alias_uninstall.set_defaults(_handler=alias_uninstall_cmd)
    return parser
