import argparse
from docx import Document
from autodocz.generator import make_readme, make_license, make_report
from autodocz.utils import *

def main():
    parser = argparse.ArgumentParser(
        prog="autodocz",
        description="autodocz: directory tree & doc generator"
    )
    sub = parser.add_subparsers(dest="command", required=True)

    # GENERATE command
    gen = sub.add_parser("generate", help="generate documentation files")
    gen.add_argument("doc", choices=["readme", "license", "srs", "report","all"],
                     help="which document to generate")
    gen.add_argument("-o", "--output", default=None,
                     help="path/filename to write (default: README.md, LICENSE, SRS.md)")

    args = parser.parse_args()

    if args.command == "generate":
        review_data = get_project_review() if args.doc in ("readme", "report", "all") else None
        targets = [args.doc] if args.doc != "all" else ["readme", "license", "report"]

        for doc in targets:
            if doc == "readme":
                content = make_readme(review_data)
                out = "README.md"
                mode = "w"
            elif doc == "license":
                content = make_license()
                out = "LICENSE"
                mode = "w"
            elif doc == "report":
                report_text = make_report(review_data) 
                doc = Document()
                doc.add_paragraph(report_text)
                out = "REPORT.docx"
                if args.doc != "all" and args.output:
                    out = args.output
                doc.save(out)
                print(f"✅ Generated {out}")
                continue 

            with open(out, mode) as f:
                f.write(content)
            print(f"✅ Generated {out}")
