#!/usr/bin/env python3
"""
Blind scoring test for Layer 3 v4 premises.
Strips all context, randomizes order, sends to Opus as TikTok scroller.
"""

import json
import random
import subprocess
import sys
from datetime import datetime

def run_blind_scoring(channel_id: str):
    # Load opportunities
    input_path = f'/home/sietch6/trending-topics-pipeline/output/analysis_opportunities_{channel_id}.json'
    output_path = f'/home/sietch6/trending-topics-pipeline/sandbox/v4_blind_scores_{channel_id}.json'

    with open(input_path) as f:
        data = json.load(f)

    opps = data.get('opportunities', [])

    # Extract premises with their original metadata (for later comparison)
    premises_data = []
    for i, opp in enumerate(opps):
        premise = opp.get('premise', opp.get('suggested_title', ''))
        if premise and premise.strip():
            premises_data.append({
                'original_index': i,
                'premise': premise,
                'original_verdict': opp.get('verdict', 'UNKNOWN'),
                'original_score': opp.get('weighted_score', 0)
            })

    print(f"Loaded {len(premises_data)} premises from {channel_id}")

    # Shuffle for blind test
    random.seed(42)  # Reproducible shuffle
    shuffled = premises_data.copy()
    random.shuffle(shuffled)

    # Build prompt with just the premises
    premises_list = "\n".join([f"{i+1}. {p['premise']}" for i, p in enumerate(shuffled)])

    prompt = f"""You are a person scrolling TikTok at 11pm. You're half paying attention, thumb ready to swipe.

For each of the following video titles, rate how likely you are to STOP SCROLLING and watch (1-10):
- 10: Physically cannot scroll past. "Wait, what?"
- 8-9: Very strong. Would stop most of the time.
- 6-7: Interesting enough to pause, might watch.
- 4-5: Mildly curious but probably keep scrolling.
- 2-3: Not compelling. Easy skip.
- 1: Actively uninteresting or off-putting.

Be honest and harsh. A 10 should be rare. Most titles should be 4-7.

Rate ONLY based on the title. No context about who made it or what channel it's from.

Here are the titles:

{premises_list}

Respond in JSON format:
{{
  "scores": [
    {{"index": 1, "score": N, "reason": "brief reason"}},
    ...
  ]
}}
"""

    print("Sending to Opus for blind scoring...")

    result = subprocess.run(
        ["claude", "--print", "--model", "opus", "-p", "-"],
        input=prompt,
        capture_output=True,
        text=True,
        timeout=600
    )

    if result.returncode != 0:
        print(f"Error: {result.stderr}")
        return

    # Parse response
    import re
    response = result.stdout.strip()
    json_match = re.search(r'\{.*\}', response, re.DOTALL)

    if not json_match:
        print("Could not parse JSON response")
        print(response[:1000])
        return

    blind_scores = json.loads(json_match.group())

    # Map back to original data
    results = []
    for score_entry in blind_scores.get('scores', []):
        idx = score_entry['index'] - 1  # 1-indexed to 0-indexed
        if 0 <= idx < len(shuffled):
            original = shuffled[idx]
            results.append({
                'premise': original['premise'],
                'blind_score': score_entry['score'],
                'blind_reason': score_entry.get('reason', ''),
                'original_verdict': original['original_verdict'],
                'original_weighted_score': original['original_score'],
                'original_index': original['original_index']
            })

    # Sort by blind score descending
    results.sort(key=lambda x: x['blind_score'], reverse=True)

    # Calculate statistics
    scores = [r['blind_score'] for r in results]
    avg_score = sum(scores) / len(scores) if scores else 0

    # Correlation analysis
    make_now_scores = [r['blind_score'] for r in results if r['original_verdict'] == 'MAKE_NOW']
    worth_making_scores = [r['blind_score'] for r in results if r['original_verdict'] == 'WORTH_MAKING']
    skip_scores = [r['blind_score'] for r in results if r['original_verdict'] == 'SKIP']

    output = {
        'channel_id': channel_id,
        'test_date': datetime.now().isoformat(),
        'total_premises': len(results),
        'statistics': {
            'average_blind_score': round(avg_score, 2),
            'avg_by_verdict': {
                'MAKE_NOW': round(sum(make_now_scores) / len(make_now_scores), 2) if make_now_scores else 0,
                'WORTH_MAKING': round(sum(worth_making_scores) / len(worth_making_scores), 2) if worth_making_scores else 0,
                'SKIP': round(sum(skip_scores) / len(skip_scores), 2) if skip_scores else 0
            },
            'score_distribution': {
                '9-10': len([s for s in scores if s >= 9]),
                '7-8': len([s for s in scores if 7 <= s < 9]),
                '5-6': len([s for s in scores if 5 <= s < 7]),
                '3-4': len([s for s in scores if 3 <= s < 5]),
                '1-2': len([s for s in scores if s < 3])
            }
        },
        'results': results
    }

    # Save results
    with open(output_path, 'w') as f:
        json.dump(output, f, indent=2)

    print(f"\nBlind scoring complete!")
    print(f"Results saved to: {output_path}")
    print(f"\nSUMMARY:")
    print(f"  Average blind score: {avg_score:.2f}")
    print(f"  MAKE_NOW avg: {output['statistics']['avg_by_verdict']['MAKE_NOW']}")
    print(f"  WORTH_MAKING avg: {output['statistics']['avg_by_verdict']['WORTH_MAKING']}")
    print(f"  SKIP avg: {output['statistics']['avg_by_verdict']['SKIP']}")
    print(f"\n  Score distribution:")
    for bucket, count in output['statistics']['score_distribution'].items():
        print(f"    {bucket}: {count}")

if __name__ == '__main__':
    channel = sys.argv[1] if len(sys.argv) > 1 else 'why_you_do_that'
    run_blind_scoring(channel)
