"""
Prompt A: Baseline (Current Production)

This is the current Layer 3 prompt adapted for sandbox testing.
Represents the control condition - how the pipeline currently works.
"""

PROMPT_A = """You are analyzing a trending topic cluster to determine if it represents a viable explainer video opportunity. Your job is to provide INSIGHT, not caution. Do not hedge. Do not be diplomatic. Tell me exactly what the opportunity is, why it exists, and how to exploit it.

I am a content creator looking for topics where I can make videos that genuinely help people understand something they're confused about. I don't need you to protect me from bad ideas — I need you to find the best ideas and tell me precisely how to execute them.

TOPIC CLUSTER:
{titles_text}

REDDIT SIGNALS:
{reddit_text}

ANALYZE THIS CLUSTER BY ANSWERING EACH SECTION:

1. THE REAL QUESTION
What are people actually confused about? Don't generalize. Look at the Reddit post titles and identify the SPECIFIC gap in understanding.

2. WHY EXISTING CONTENT FAILS
What YouTube videos and articles already exist on this topic? Be specific about what they get wrong or what they miss.

3. THE WINNING ANGLE
What specific angle would make a NEW video on this topic succeed?

CRITICAL FRAMING PRINCIPLE: The viewer must feel SMARTER, more validated, or more empowered after watching — never more anxious, broken, or guilty.

State your angle as a single sentence: "The video that wins is the one that..."

4. SCORING (1-5 each)

Demand Signal (25%): Is there proven search/engagement demand?
Content Gap (25%): How well is this currently served by existing content?
Explainability (15%): Can this be explained satisfyingly in one video?
Evergreen Potential (15%): Will this still get searches in 6-12 months?
Audience Breadth (10%): How many people could realistically care?
Competition (10%): How hard to break through on YouTube?

5. WEIGHTED SCORE: Calculate it. Show the math: (demand * 0.25) + (gap * 0.25) + (explain * 0.15) + (evergreen * 0.15) + (breadth * 0.10) + (competition * 0.10) = X

6. CONTENT BRIEF
- Title: Specific, compelling, uses validation framing
- Opening hook: The first 15 seconds that stops someone from scrolling
- The one thing that must be right: What single execution detail determines success or failure?
- Viewer emotional payoff: How does the viewer feel after watching? (empowered, validated, smarter, relieved, or amused)

7. VERDICT — Choose exactly one:
- HIGH_PRIORITY: Clear demand, clear gap, strong angle. Make this.
- WORTH_MAKING: Solid opportunity, no urgency. Add to production queue.
- CONDITIONAL: Viable only if a specific condition is met. State it.
- SKIP: Not worth production effort. One sentence why.

Respond in JSON with these fields:
{{
  "theme": "The core theme of this cluster",
  "the_question": "What people are confused about",
  "winning_angle": "The video that wins is the one that...",
  "suggested_title": "Your proposed video title",
  "opening_hook": "First 15 seconds of the video",
  "scores": {{
    "demand_signal": {{"score": X, "reasoning": "..."}},
    "content_gap": {{"score": X, "reasoning": "..."}},
    "explainability": {{"score": X, "reasoning": "..."}},
    "evergreen_potential": {{"score": X, "reasoning": "..."}},
    "audience_breadth": {{"score": X, "reasoning": "..."}},
    "competition": {{"score": X, "reasoning": "..."}}
  }},
  "weighted_score": X.XX,
  "emotional_payoff": "How viewer feels (one of: empowered, validated, smarter, relieved, amused)",
  "verdict": "HIGH_PRIORITY / WORTH_MAKING / CONDITIONAL / SKIP",
  "verdict_reasoning": "One sentence"
}}

Respond in JSON only. No markdown code blocks."""


def format_prompt_a(cluster_data: dict) -> str:
    """Format Prompt A (baseline) with cluster data."""
    # Build titles text
    titles = [t.get('topic_name', t.get('title', '')) for t in cluster_data.get('topics', [])]
    if not titles:
        titles = [p.get('title', '') for p in cluster_data.get('raw_posts', [])]
    titles_text = "\n".join([f"- {t}" for t in titles[:30]])

    # Build reddit text (baseline format - more abbreviated)
    reddit_lines = []
    for post in cluster_data.get('raw_posts', [])[:15]:
        title = post.get('title', '')
        selftext = post.get('selftext', '')[:200]  # Truncated like baseline
        subreddit = post.get('subreddit', '')
        num_comments = post.get('num_comments', 0)
        score = post.get('score', 0)

        reddit_lines.append(f"- r/{subreddit}: \"{title}\" (score: {score}, comments: {num_comments})")
        if selftext:
            reddit_lines.append(f"  Context: {selftext}...")

    reddit_text = "\n".join(reddit_lines) if reddit_lines else "No Reddit posts in this cluster."

    return PROMPT_A.format(
        titles_text=titles_text,
        reddit_text=reddit_text
    )


if __name__ == "__main__":
    # Test with sample data
    import json
    with open('test_clusters.json') as f:
        data = json.load(f)

    cluster = data['clusters'][0]
    prompt = format_prompt_a(cluster)
    print(prompt[:2000])
    print("...")
    print(f"\nTotal prompt length: {len(prompt)} chars")
