#!/usr/bin/env python3
"""Extract identity signals from an opportunity email body/signature.

Usage:
  PYTHONPATH=. python scripts/extract_email_identity.py --opportunity-id <uuid>
  PYTHONPATH=. python scripts/extract_email_identity.py --text-file /tmp/email.txt --email user@example.pt
"""
from __future__ import annotations

import argparse
import json
from pathlib import Path

from app.email_identity_extraction_service import extract_email_identity, extract_identity_for_opportunity


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--opportunity-id")
    parser.add_argument("--text-file")
    parser.add_argument("--email", default="")
    parser.add_argument("--subject", default="")
    parser.add_argument("--no-llm", action="store_true")
    parser.add_argument("--refresh", action="store_true")
    args = parser.parse_args()

    if args.opportunity_id:
        result = extract_identity_for_opportunity(args.opportunity_id, refresh=args.refresh, use_llm=not args.no_llm)
    elif args.text_file:
        result = extract_email_identity(
            Path(args.text_file).read_text(encoding="utf-8"),
            email=args.email,
            subject=args.subject,
            use_llm=not args.no_llm,
        )
    else:
        parser.error("use --opportunity-id or --text-file")

    print(json.dumps(result or {}, ensure_ascii=False, indent=2, default=str))


if __name__ == "__main__":
    main()
