import os
import sys
import json
import argparse
import anthropic

from translate_lecture.translation import generate_config


def main():
    parser = argparse.ArgumentParser(
        prog="translate-lecture-init-config",
        description="Generate an initial JSON config from PPTX lecture files.",
    )
    parser.add_argument("pptx_files", nargs="+", help="PPTX files to analyse")
    parser.add_argument("--target-language", required=True,
                        help="Target translation language, e.g. 'German'")
    parser.add_argument("--output", default=None,
                        help="Output JSON path (default: <first-pptx-stem>.json)")
    parser.add_argument("--model", default="claude-sonnet-4-6",
                        help="Claude model to use (default: claude-sonnet-4-6)")
    args = parser.parse_args()

    for path in args.pptx_files:
        if not os.path.exists(path):
            print(f"ERROR: PPTX file not found: {path}")
            sys.exit(1)

    output = args.output or (os.path.splitext(args.pptx_files[0])[0] + ".json")
    if os.path.exists(output):
        print(f"ERROR: output file already exists: {output}")
        sys.exit(1)

    api_key = os.environ.get("ANTHROPIC_API_KEY")
    if not api_key:
        print("ERROR: ANTHROPIC_API_KEY environment variable not set.")
        sys.exit(1)

    client = anthropic.Anthropic(api_key=api_key)
    cfg = generate_config(client, args.model, args.pptx_files, args.target_language)

    with open(output, "w", encoding="utf-8") as f:
        json.dump(cfg, f, ensure_ascii=False, indent=2)
    print(f"Config written to: {output}")
    print("Review and adjust the generated config before running translation.")
