from __future__ import annotations

import argparse


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="AIPM CLI")
    subparsers = parser.add_subparsers(dest="command", required=True)
    init_parser = subparsers.add_parser("init")
    init_parser.add_argument("--host", default="")
    init_parser.add_argument("--port", type=int, default=None)
    subparsers.add_parser("help")
    subparsers.add_parser("info")
    subparsers.add_parser("doctor")
    subparsers.add_parser("status")
    subparsers.add_parser("inbox")
    subparsers.add_parser("context")
    subparsers.add_parser("next")
    subparsers.add_parser("handoff")
    code = subparsers.add_parser("code").add_subparsers(dest="code_command", required=True)
    code.add_parser("status")
    code_diff = code.add_parser("diff")
    code_diff.add_argument("--staged", action="store_true")
    code_recent = code.add_parser("recent")
    code_recent.add_argument("--limit", type=int, default=10)
    brief = subparsers.add_parser("brief").add_subparsers(dest="brief_command", required=True)
    brief.add_parser("product")
    brief.add_parser("architecture")
    brief.add_parser("modules")
    canon = subparsers.add_parser("canon").add_subparsers(dest="canon_command", required=True)
    canon.add_parser("show")
    canon_update = canon.add_parser("update")
    canon_update.add_argument("--decision-id", required=True)
    canon_update.add_argument("--product-goal", default="")
    canon_update.add_argument("--engineering-focus", default="")
    canon_update.add_argument("--architecture", default="")
    canon_update.add_argument("--add-scope", nargs="*", default=[])
    canon_update.add_argument("--add-avoid", nargs="*", default=[])
    vision = subparsers.add_parser("vision").add_subparsers(dest="vision_command", required=True)
    vision_list = vision.add_parser("list")
    vision_list.add_argument("--status", default="")
    vision_show = vision.add_parser("show")
    vision_show.add_argument("--id", required=True)
    vision_add = vision.add_parser("add")
    vision_add.add_argument("--title", required=True)
    vision_add.add_argument("--summary", default="")
    vision_add.add_argument("--status", default="active")
    vision_add.add_argument("--horizon", default="long_term")
    vision_update = vision.add_parser("update")
    vision_update.add_argument("--id", required=True)
    vision_update.add_argument("--title", default=None)
    vision_update.add_argument("--summary", default=None)
    vision_update.add_argument("--status", default=None)
    vision_update.add_argument("--horizon", default=None)

    roadmap = subparsers.add_parser("roadmap").add_subparsers(dest="roadmap_command", required=True)
    roadmap_list = roadmap.add_parser("list")
    roadmap_list.add_argument("--vision-id", default=None)
    roadmap_show = roadmap.add_parser("show")
    roadmap_show.add_argument("--id", required=True)
    roadmap_add = roadmap.add_parser("add")
    roadmap_add.add_argument("--title", required=True)
    roadmap_add.add_argument("--target-date", default="")
    roadmap_add.add_argument("--vision-id", default=None)
    roadmap_add.add_argument("--status", default="planned")
    roadmap_add.add_argument("--priority", default="P1")
    roadmap_update = roadmap.add_parser("update")
    roadmap_update.add_argument("--id", required=True)
    roadmap_update.add_argument("--title", default=None)
    roadmap_update.add_argument("--target-date", default=None)
    roadmap_update.add_argument("--status", default=None)
    roadmap_update.add_argument("--priority", default=None)

    plan = subparsers.add_parser("plan").add_subparsers(dest="plan_command", required=True)
    plan_list = plan.add_parser("list")
    plan_list.add_argument("--roadmap-id", default=None)
    plan_list.add_argument("--status", default=None)
    plan_show = plan.add_parser("show")
    plan_show.add_argument("--id", required=True)
    plan_packet = plan.add_parser("packet")
    plan_packet.add_argument("--id", required=True)
    plan_packet.add_argument("--format", default="prompt")
    plan_advance = plan.add_parser("advance")
    plan_advance.add_argument("--id", required=True)
    plan_add = plan.add_parser("add")
    plan_add.add_argument("--title", required=True)
    plan_add.add_argument("--goal", default="")
    plan_add.add_argument("--roadmap-id", default=None, dest="roadmap_id")
    plan_add.add_argument("--vision-id", default=None, dest="vision_id")
    plan_add.add_argument("--priority", default="P1")
    plan_add.add_argument("--status", default="draft")
    plan_add.add_argument("--scope", nargs="*", default=[])
    plan_add.add_argument("--risks", nargs="*", default=[])
    plan_add.add_argument("--assumptions", nargs="*", default=[])
    plan_add.add_argument("--task-ids", nargs="*", default=[], dest="task_ids")
    plan_generate = plan.add_parser("generate")
    plan_generate.add_argument("--roadmap-id", default=None, dest="roadmap_id")
    plan_generate.add_argument("--vision-id", default=None, dest="vision_id")
    plan_generate.add_argument("--title", default="")
    plan_generate.add_argument("--task-limit", type=int, default=4, dest="task_limit")
    plan_generate.add_argument("--create-tasks", action="store_true", dest="create_tasks")
    plan_update = plan.add_parser("update")
    plan_update.add_argument("--id", required=True)
    plan_update.add_argument("--title", default=None)
    plan_update.add_argument("--goal", default=None)
    plan_update.add_argument("--status", default=None)
    plan_update.add_argument("--priority", default=None)
    plan_update.add_argument("--task-ids", nargs="*", default=None, dest="task_ids")

    principle = subparsers.add_parser("principle").add_subparsers(dest="principle_command", required=True)
    principle_list = principle.add_parser("list")
    principle_list.add_argument("--status", default="")
    principle_list.add_argument("--kind", default="")
    principle_show = principle.add_parser("show")
    principle_show.add_argument("--id", required=True)
    principle_add = principle.add_parser("add")
    principle_add.add_argument("--title", required=True)
    principle_add.add_argument("--summary", default="")
    principle_add.add_argument("--kind", default="governance")
    principle_add.add_argument("--status", default="active")
    principle_update = principle.add_parser("update")
    principle_update.add_argument("--id", required=True)
    principle_update.add_argument("--title", default=None)
    principle_update.add_argument("--summary", default=None)
    principle_update.add_argument("--kind", default=None)
    principle_update.add_argument("--status", default=None)
    link = subparsers.add_parser("link").add_subparsers(dest="link_command", required=True)
    link_list = link.add_parser("list")
    link_list.add_argument("--source-id", default="")
    link_list.add_argument("--target-id", default="")
    link_list.add_argument("--relation", default="")
    link_add = link.add_parser("add")
    link_add.add_argument("--source-type", required=True)
    link_add.add_argument("--source-id", required=True)
    link_add.add_argument("--relation", required=True)
    link_add.add_argument("--target-type", required=True)
    link_add.add_argument("--target-id", required=True)
    link_add.add_argument("--note", default="")
    link_delete = link.add_parser("delete")
    link_delete.add_argument("--id", required=True)
    decision = subparsers.add_parser("decision").add_subparsers(dest="decision_command", required=True)
    decision.add_parser("list")
    decision_show = decision.add_parser("show")
    decision_show.add_argument("--id", required=True)
    decision_add = decision.add_parser("add")
    decision_add.add_argument("--title", required=True)
    decision_add.add_argument("--background", required=True)
    decision_add.add_argument("--decision", required=True)
    decision_add.add_argument("--status", default="proposed")
    decision_review = decision.add_parser("review")
    decision_review.add_argument("--id", required=True)
    decision_review.add_argument("--status", required=True)
    idea = subparsers.add_parser("idea").add_subparsers(dest="idea_command", required=True)
    idea_list = idea.add_parser("list")
    idea_list.add_argument("--status", default="")
    idea_show = idea.add_parser("show")
    idea_show.add_argument("--id", required=True)
    idea_capture = idea.add_parser("capture")
    idea_capture.add_argument("--title", required=True)
    idea_capture.add_argument("--summary", required=True)
    idea_capture.add_argument("--impact", default="")
    idea_capture.add_argument("--source", default="manual")
    idea_capture.add_argument("--canon-conflict", action="store_true", dest="canon_conflict")
    idea_capture.add_argument("--current-summary", default="", dest="current_summary")
    idea_capture.add_argument("--main-question", default="", dest="main_question")
    idea_capture.add_argument("--recommended-next-action", default="continue_discussion", dest="recommended_next_action")
    idea_review = idea.add_parser("review")
    idea_review.add_argument("--id", required=True)
    idea_review.add_argument("--status", required=True)
    idea_review.add_argument("--note", default="")
    idea_update = idea.add_parser("update")
    idea_update.add_argument("--id", required=True)
    idea_update.add_argument("--title", default=None)
    idea_update.add_argument("--summary", default=None)
    idea_update.add_argument("--impact", default=None)
    idea_update.add_argument("--source", default=None)
    idea_update.add_argument("--status", default=None)
    idea_update.add_argument("--current-summary", default=None, dest="current_summary")
    idea_update.add_argument("--main-question", default=None, dest="main_question")
    idea_update.add_argument("--recommended-next-action", default=None, dest="recommended_next_action")
    idea_comment = idea.add_parser("comment")
    idea_comment.add_argument("--id", required=True)
    idea_comment.add_argument("--content", required=True)
    idea_comment.add_argument("--kind", default="comment")
    idea_comment.add_argument("--author-type", default="ai", dest="author_type")
    idea_comment.add_argument("--author-name", default="aipmc", dest="author_name")
    idea_convert = idea.add_parser("convert")
    idea_convert.add_argument("--id", required=True)
    idea_convert.add_argument("--to", required=True, dest="target_type", choices=["task", "decision"])
    task = subparsers.add_parser("task").add_subparsers(dest="task_command", required=True)
    task_list = task.add_parser("list")
    task_list.add_argument("--status", default="")
    task_show = task.add_parser("show")
    task_show.add_argument("--id", required=True)
    task_add = task.add_parser("add")
    task_add.add_argument("--title", required=True)
    task_add.add_argument("--priority", default="P1")
    task_add.add_argument("--status", default="todo")
    task_add.add_argument("--phase", default="general")
    task_add.add_argument("--roadmap-id", default=None, dest="roadmap_id")
    task_add.add_argument("--plan-id", default=None, dest="plan_id")
    task_add.add_argument("--acceptance", nargs="*", default=[])
    task_update = task.add_parser("update")
    task_update.add_argument("--id", required=True)
    task_update.add_argument("--status", required=True)
    task_update.add_argument("--note", default="")
    task_update.add_argument("--allow-without-commit", action="store_true", dest="allow_without_commit")
    task_update.add_argument("--roadmap-id", default=None, dest="roadmap_id")
    task_update.add_argument("--plan-id", default=None, dest="plan_id")
    task_plan = task.add_parser("plan")
    task_plan.add_argument("--id", required=True)
    task_plan.add_argument("--steps", nargs="+", required=True)
    task_checkpoint = task.add_parser("checkpoint")
    task_checkpoint.add_argument("--id", required=True)
    task_checkpoint.add_argument("--index", type=int, required=True)
    task_checkpoint.add_argument("--done", action="store_true", default=True)
    task_checkpoint.add_argument("--not-done", action="store_false", dest="done")
    commit = subparsers.add_parser("commit").add_subparsers(dest="commit_command", required=True)
    commit_list = commit.add_parser("list")
    commit_list.add_argument("--status", default="")
    commit_list.add_argument("--task-id", default="", dest="task_id")
    commit_list.add_argument("--decision-id", default="", dest="decision_id")
    commit_show = commit.add_parser("show")
    commit_show.add_argument("--id", required=True)
    commit_add = commit.add_parser("add")
    commit_add.add_argument("--title", required=True)
    commit_add.add_argument("--summary", default="")
    commit_add.add_argument("--evidence-summary", default="", dest="evidence_summary")
    commit_add.add_argument("--review-notes", default="", dest="review_notes")
    commit_add.add_argument("--branch", default="")
    commit_add.add_argument("--commit-hash", default="", dest="commit_hash")
    commit_add.add_argument("--task-id", default="", dest="task_id")
    commit_add.add_argument("--decision-id", default="", dest="decision_id")
    commit_add.add_argument("--status", default="draft")
    commit_add.add_argument("--test-status", default="not_run", dest="test_status")
    commit_add.add_argument("--review-status", default="pending", dest="review_status")
    commit_add.add_argument("--files", nargs="*", default=[])
    commit_add.add_argument("--auto-git", action="store_true", dest="auto_git")
    commit_update = commit.add_parser("update")
    commit_update.add_argument("--id", required=True)
    commit_update.add_argument("--title", default=None)
    commit_update.add_argument("--summary", default=None)
    commit_update.add_argument("--evidence-summary", default=None, dest="evidence_summary")
    commit_update.add_argument("--review-notes", default=None, dest="review_notes")
    commit_update.add_argument("--branch", default=None)
    commit_update.add_argument("--commit-hash", default=None, dest="commit_hash")
    commit_update.add_argument("--task-id", default=None, dest="task_id")
    commit_update.add_argument("--decision-id", default=None, dest="decision_id")
    commit_update.add_argument("--status", default=None)
    commit_update.add_argument("--test-status", default=None, dest="test_status")
    commit_update.add_argument("--review-status", default=None, dest="review_status")
    commit_update.add_argument("--files", nargs="*", default=None)
    commit_update.add_argument("--auto-git", action="store_true", dest="auto_git")
    commit_update.add_argument("--clear-task-id", action="store_true", dest="clear_task_id")
    commit_update.add_argument("--clear-decision-id", action="store_true", dest="clear_decision_id")
    daily = subparsers.add_parser("daily").add_subparsers(dest="daily_command", required=True)
    daily_show = daily.add_parser("show")
    daily_show.add_argument("--date", default=None)
    daily_close = daily.add_parser("close")
    daily_close.add_argument("--completed", nargs="*", default=[])
    daily_close.add_argument("--problems", nargs="*", default=[])
    daily_close.add_argument("--risks", nargs="*", default=[])
    daily_close.add_argument("--next", nargs="*", default=[])
    daily_replace = daily.add_parser("replace")
    daily_replace.add_argument("--date", default=None)
    daily_replace.add_argument("--completed", nargs="*", default=[])
    daily_replace.add_argument("--problems", nargs="*", default=[])
    daily_replace.add_argument("--risks", nargs="*", default=[])
    daily_replace.add_argument("--next", nargs="*", default=[])
    docs = subparsers.add_parser("docs").add_subparsers(dest="docs_command", required=True)
    docs.add_parser("sync")
    docs.add_parser("repair")
    docs.add_parser("prune")
    docs_list = docs.add_parser("list")
    docs_list.add_argument("--status", default="")
    docs_list.add_argument("--layer", default="")
    docs_update = docs.add_parser("update")
    docs_update.add_argument("--path", required=True)
    docs_update.add_argument("--type", default="")
    docs_update.add_argument("--status", default="")
    docs_update.add_argument("--layer", default="")
    docs_update.add_argument("--source-of-truth", action="store_true", dest="source_of_truth")
    docs_update.add_argument("--clear-source-of-truth", action="store_true", dest="clear_source_of_truth")
    docs_update.add_argument("--superseded-by", default=None, dest="superseded_by")
    docs_update.add_argument("--create", action="store_true", dest="create")
    docs_audit = docs.add_parser("audit")
    feedback = subparsers.add_parser("feedback").add_subparsers(dest="feedback_command", required=True)
    feedback_list = feedback.add_parser("list")
    feedback_list.add_argument("--base-url", default=None, dest="base_url")
    feedback_add = feedback.add_parser("add")
    feedback_add.add_argument("--label", required=True, choices=["bug", "suggestion"])
    feedback_add.add_argument("--content", required=True)
    feedback_add.add_argument("--base-url", default=None, dest="base_url")
    return parser
