#!/usr/bin/env python3
"""Test Visual Decider with varied data signals - not hardcoded in prompt."""

import json
import subprocess
import re

from prompts.prompt_v4.layer4_visual_decider import format_prompt

TEST_CASES = [
    # These phrasings are NOT in the prompt examples
    ("increased fivefold", "The company's market share increased fivefold between 2010 and 2020."),
    ("went from X to Y", "Obesity rates went from 13% to 42% in just two decades."),
    ("dropped by half", "After the regulation, emissions dropped by half within five years."),
    ("skyrocketed", "Housing prices skyrocketed while wages remained flat."),
    # Control: should NOT be data viz
    ("biographical fact", "She was born in a small village in 1892."),
    ("single number no trend", "The building stands 432 feet tall."),
]

def call_claude_json(prompt: str) -> dict:
    """Call Claude and parse JSON response."""
    try:
        result = subprocess.run(
            ["claude", "--print", prompt, "--model", "sonnet", "--output-format", "json"],
            capture_output=True,
            text=True,
            timeout=120  # Increased timeout
        )
        wrapper = json.loads(result.stdout.strip())
        content = wrapper.get("result", result.stdout)
        content = re.sub(r'^```json\s*', '', str(content))
        content = re.sub(r'\s*```$', '', content)
        json_match = re.search(r'\{[\s\S]*\}', content)
        if json_match:
            return json.loads(json_match.group())
    except Exception as e:
        print(f"Error: {e}")
    return {}

print("=" * 70)
print("TESTING VISUAL DECIDER WITH VARIED DATA SIGNALS")
print("=" * 70)

for label, voiceover in TEST_CASES:
    prompt = format_prompt(
        segment_id="test",
        time_start=0,
        time_end=5,
        beat_name="Test Beat",
        voiceover=voiceover,
        premise="Test documentary about trends and statistics",
        research_excerpt="(No specific research)"
    )
    
    result = call_claude_json(prompt)
    decision = result.get("decision", "ERROR")
    thinking = result.get("thinking", "")[:100]
    
    # Get data viz details if applicable
    dv = result.get("data_visualization", {})
    
    print(f"\n[{label}]")
    print(f"  VO: \"{voiceover}\"")
    print(f"  DECISION: {decision}")
    if decision == "DATA_VISUALIZATION" and dv:
        print(f"  CHART: {dv.get('chart_type')}")
        print(f"  DATA: {dv.get('data', [])[:2]}...")
    print(f"  THINKING: {thinking}...")

print("\n" + "=" * 70)
print("EXPECTED: First 4 should be DATA_VISUALIZATION, last 2 should be IMAGE")
print("=" * 70)
