from __future__ import annotations

import argparse
import sys
from pathlib import Path

ROOT_DIR = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT_DIR))

from app.review_filler import fill_review_docx_from_analysis, write_decisions_json


def build_arg_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Fill Appendix A DOCX review results from an analysis Markdown file.")
    parser.add_argument("--analysis-md", type=Path, required=True, help="Path to the analysis Markdown file.")
    parser.add_argument("--review-docx", type=Path, required=True, help="Path to the Appendix A review DOCX file.")
    parser.add_argument("--output-docx", type=Path, required=True, help="Path for the filled review DOCX file.")
    parser.add_argument("--output-json", type=Path, help="Optional path for review decision details.")
    parser.add_argument("--target-heading", help="Optional review table heading filter, such as A.2.")
    return parser


def main() -> None:
    args = build_arg_parser().parse_args()
    result = fill_review_docx_from_analysis(
        analysis_markdown_path=args.analysis_md,
        review_docx_path=args.review_docx,
        output_docx_path=args.output_docx,
        target_heading=args.target_heading,
    )
    if args.output_json:
        write_decisions_json(result, args.output_json)

    counts: dict[str, int] = {}
    for decision in result.decisions:
        counts[decision.result] = counts.get(decision.result, 0) + 1

    print(f"target_heading={result.target_heading}")
    print(f"decisions={len(result.decisions)}")
    for result_name in ("通过", "未通过", "不适用"):
        print(f"{result_name}={counts.get(result_name, 0)}")
    print(f"output_docx={result.output_docx}")
    if args.output_json:
        print(f"output_json={args.output_json}")


if __name__ == "__main__":
    main()

"""
python scripts/fill_review_docx.py --analysis-md test/中央处理机正常模式软件任务书V1_00_094006f6_analysis.md --review-docx test/附录A文档审查.docx --output-docx test/中央处理机正
  │ 常模式软件任务书V1_00_094006f6_附录A文档审查.docx --output-json test/中央处理机正常模式软件任务书V1_00_094006f6_附录A文档审查.json
"""


