"""GitHub command argument definitions.

This module defines ALL arguments for the github command in ONE place.
Both github_scraper.py (standalone) and parsers/github_parser.py (unified CLI)
import and use these definitions.

This ensures the parsers NEVER drift out of sync.

Shared arguments (name, description, output, enhance-level, api-key,
dry-run, verbose, quiet, workflow args) come from common.py / workflow.py
via ``add_all_standard_arguments()``.
"""

import argparse
from typing import Any

from .common import add_all_standard_arguments

# GitHub-specific argument definitions as data structure
# NOTE: Shared args (name, description, enhance_level, api_key, dry_run,
#       verbose, quiet, workflow args) are registered by add_all_standard_arguments().
GITHUB_ARGUMENTS: dict[str, dict[str, Any]] = {
    # Core GitHub options
    "repo": {
        "flags": ("--repo",),
        "kwargs": {
            "type": str,
            "help": "GitHub repository (owner/repo)",
            "metavar": "OWNER/REPO",
        },
    },
    "config": {
        "flags": ("--config",),
        "kwargs": {
            "type": str,
            "help": "Path to config JSON file",
            "metavar": "FILE",
        },
    },
    "token": {
        "flags": ("--token",),
        "kwargs": {
            "type": str,
            "help": "GitHub personal access token",
            "metavar": "TOKEN",
        },
    },
    # Content options
    "no_issues": {
        "flags": ("--no-issues",),
        "kwargs": {
            "action": "store_true",
            "help": "Skip GitHub issues",
        },
    },
    "no_changelog": {
        "flags": ("--no-changelog",),
        "kwargs": {
            "action": "store_true",
            "help": "Skip CHANGELOG",
        },
    },
    "no_releases": {
        "flags": ("--no-releases",),
        "kwargs": {
            "action": "store_true",
            "help": "Skip releases",
        },
    },
    "max_issues": {
        "flags": ("--max-issues",),
        "kwargs": {
            "type": int,
            "default": 100,
            "help": "Max issues to fetch (default: 100)",
            "metavar": "N",
        },
    },
    "since": {
        "flags": ("--since",),
        "kwargs": {
            "type": str,
            "default": None,
            "help": "Only fetch issues updated after this date (ISO8601, UTC 'Z' suffix accepted, or YYYY-MM-DD; e.g. 2026-01-01T00:00:00Z)",
            "metavar": "DATE",
        },
    },
    "issue_labels": {
        "flags": ("--issue-labels",),
        "kwargs": {
            "type": str,
            "default": None,
            "help": "Filter issues by labels (comma-separated, e.g. bug,enhancement)",
            "metavar": "LABELS",
        },
    },
    "issue_state": {
        "flags": ("--issue-state",),
        "kwargs": {
            "type": str,
            "default": None,
            "choices": ["open", "closed", "all"],
            "help": "Filter issues by state (default: all)",
        },
    },
    "max_comments": {
        "flags": ("--max-comments",),
        "kwargs": {
            "type": int,
            "default": 0,
            "help": "Fetch up to N comments per issue (default: 0, disabled; costs 1+ extra API call per issue, paginated)",
            "metavar": "N",
        },
    },
    "per_issue_files": {
        "flags": ("--per-issue-files",),
        "kwargs": {
            "action": "store_true",
            "help": "Write individual markdown files per issue (with YAML frontmatter)",
        },
    },
    # Control options
    "scrape_only": {
        "flags": ("--scrape-only",),
        "kwargs": {
            "action": "store_true",
            "help": "Only scrape, don't build skill",
        },
    },
    # Mode options
    "non_interactive": {
        "flags": ("--non-interactive",),
        "kwargs": {
            "action": "store_true",
            "help": "Non-interactive mode for CI/CD (fail fast on rate limits)",
        },
    },
    "profile": {
        "flags": ("--profile",),
        "kwargs": {
            "type": str,
            "help": "GitHub profile name to use from config",
            "metavar": "NAME",
        },
    },
    "local_repo_path": {
        "flags": ("--local-repo-path",),
        "kwargs": {
            "type": str,
            "help": "Path to local clone of the repository for unlimited C3.x analysis (bypasses GitHub API file limits)",
            "metavar": "PATH",
        },
    },
}


def add_github_arguments(parser: argparse.ArgumentParser) -> None:
    """Add all github command arguments to a parser.

    This is the SINGLE SOURCE OF TRUTH for github arguments.
    Used by:
    - github_scraper.py (standalone scraper)
    - parsers/github_parser.py (unified CLI)

    Registers shared args (name, description, output, enhance-level, api-key,
    dry-run, verbose, quiet, workflow args) via add_all_standard_arguments(),
    then adds GitHub-specific args on top.

    Args:
        parser: The ArgumentParser to add arguments to

    Example:
        >>> parser = argparse.ArgumentParser()
        >>> add_github_arguments(parser)  # Adds all github args
    """
    # Shared universal args first
    add_all_standard_arguments(parser)

    # GitHub-specific args
    for arg_name, arg_def in GITHUB_ARGUMENTS.items():
        flags = arg_def["flags"]
        kwargs = arg_def["kwargs"]
        parser.add_argument(*flags, **kwargs)


def get_github_argument_names() -> set:
    """Get the set of github argument destination names.

    Returns:
        Set of argument dest names (includes shared + github-specific)
    """
    from .common import get_all_standard_argument_names

    return get_all_standard_argument_names() | set(GITHUB_ARGUMENTS.keys())


def get_github_argument_count() -> int:
    """Get the total number of github arguments.

    Returns:
        Number of arguments
    """
    from .common import COMMON_ARGUMENTS, BEHAVIOR_ARGUMENTS
    from .workflow import WORKFLOW_ARGUMENTS

    return (
        len(GITHUB_ARGUMENTS)
        + len(COMMON_ARGUMENTS)
        + len(BEHAVIOR_ARGUMENTS)
        + len(WORKFLOW_ARGUMENTS)
    )
