"""Parser assembly for the ICSD CLI."""

from __future__ import annotations

import argparse

from icsd import __version__

from ._demo import DEFAULT_DEMO_OUTPUT
from ._lint import LINT_BASELINE_PROFILE, LINT_PROFILES
from ._shared import CLI_PROG_NAME, OUTPUT_FORMATS


def build_parser() -> argparse.ArgumentParser:
    """Build the CLI parser."""
    parser = argparse.ArgumentParser(prog=CLI_PROG_NAME, description=f"ICSD CLI v{__version__}")
    parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
    subparsers = parser.add_subparsers(dest="command")

    demo_parser = subparsers.add_parser(
        "demo",
        help="Write a deterministic quick-start evidence JSON file.",
    )
    demo_parser.add_argument(
        "output",
        nargs="?",
        default=DEFAULT_DEMO_OUTPUT,
        help=f"Output path for demo evidence (default: ./{DEFAULT_DEMO_OUTPUT}).",
    )

    verify_parser = subparsers.add_parser(
        "verify",
        help="Validate evidence envelope schema and hash fields.",
    )
    verify_parser.add_argument("evidence_json", help="Path to evidence JSON envelope.")
    verify_parser.add_argument(
        "--format",
        choices=OUTPUT_FORMATS,
        default="text",
        dest="output_format",
        help="Output format for verify results (default: text).",
    )

    lint_parser = subparsers.add_parser("lint", help="Run basic static checks.")
    lint_parser.add_argument("path", help="Path to lint.")
    lint_parser.add_argument(
        "--profile",
        choices=LINT_PROFILES,
        default=LINT_BASELINE_PROFILE,
        help="Lint profile to apply (default: baseline).",
    )
    lint_parser.add_argument(
        "--format",
        choices=OUTPUT_FORMATS,
        default="text",
        dest="output_format",
        help="Output format for lint results (default: text).",
    )
    lint_parser.add_argument(
        "--jobs",
        type=int,
        default=1,
        help=(
            "Number of lint workers (default: 1, single-threaded; >1 enables "
            "opt-in deterministic parallel linting)."
        ),
    )
    return parser
