#!/usr/bin/env python3
"""Parse all 5 DT Gold raw JSON responses into markdown reports."""
import json
import os

REPORTS_DIR = "/mnt/e/genesis-system/reports"

PROMPTS = {
    1: "DT_GOLD_1_WEBSITE_GEN_PIPELINE_2026_02_21",
    2: "DT_GOLD_2_DOMAIN_PORTFOLIO_2026_02_21",
    3: "DT_GOLD_3_GHL_SAAS_PRO_2026_02_21",
    4: "DT_GOLD_4_SOCIAL_MEDIA_ENGINE_2026_02_21",
    5: "DT_GOLD_5_EMAIL_INFRASTRUCTURE_2026_02_21",
}

TITLES = {
    1: "Website Generation Pipeline",
    2: "Domain Portfolio Activation",
    3: "GHL SaaS Pro Activation",
    4: "Social Media Content Engine",
    5: "Email Infrastructure & Sequences",
}

total_cost = 0.0
total_tokens = 0

for num in range(1, 6):
    name = PROMPTS[num]
    title = TITLES[num]
    raw_file = os.path.join(REPORTS_DIR, f"{name}_RAW.json")
    output_file = os.path.join(REPORTS_DIR, f"{name}_RESPONSE_2026_02_21.md")

    print(f"\n[DT {num}] Processing {title}...")

    try:
        with open(raw_file, 'r') as f:
            data = json.load(f)

        if 'choices' in data and len(data['choices']) > 0:
            content = data['choices'][0]['message']['content']
            usage = data.get('usage', {})
            model = data.get('model', 'unknown')

            prompt_tokens = usage.get('prompt_tokens', 0)
            completion_tokens = usage.get('completion_tokens', 0)
            tok_total = usage.get('total_tokens', 0)

            # DeepSeek-R1 pricing: input $0.55/MTok, output $2.19/MTok
            input_cost = (prompt_tokens / 1_000_000) * 0.55
            output_cost = (completion_tokens / 1_000_000) * 2.19
            cost = input_cost + output_cost

            total_cost += cost
            total_tokens += tok_total

            header = f"""# DT Gold {num} Response — {title}

## Model: {model}
## Tokens: {prompt_tokens:,} input + {completion_tokens:,} output = {tok_total:,} total
## Cost: ${input_cost:.4f} input + ${output_cost:.4f} output = ${cost:.4f} total

---

"""
            with open(output_file, 'w') as f:
                f.write(header + content)

            print(f"  Model: {model}")
            print(f"  Tokens: {prompt_tokens:,} input + {completion_tokens:,} output = {tok_total:,} total")
            print(f"  Cost: ${cost:.4f}")
            print(f"  Content length: {len(content):,} chars")
            print(f"  Saved to: {output_file}")

        elif 'error' in data:
            error_msg = data['error'].get('message', str(data['error']))
            with open(output_file, 'w') as f:
                f.write(f"# ERROR\n\n{error_msg}")
            print(f"  ERROR: {error_msg}")

        else:
            with open(output_file, 'w') as f:
                f.write(f"# UNEXPECTED RESPONSE\n\n```json\n{json.dumps(data, indent=2)}\n```")
            print(f"  UNEXPECTED response format")

    except Exception as e:
        print(f"  PARSE ERROR: {e}")
        with open(output_file, 'w') as f:
            f.write(f"# PARSE ERROR\n\n{e}")

print(f"\n{'='*50}")
print(f"TOTAL: {total_tokens:,} tokens, ${total_cost:.4f}")
print(f"{'='*50}")
