import os
import json
import argparse
from openai import OpenAI

if "OPENAI_API_KEY" not in os.environ:
    raise EnvironmentError("Missing OPENAI_API_KEY environment variable.")

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# SCHEMAS must exist here or be imported
# from schemas import SCHEMAS

SCHEMAS = [
    {
        "name": "Exclusivity Signal",
        "template": "I {exclusive_action} for {this_unique_thing}.",
        "description": "Signals commitment and self-signaling through decisive exclusivity.",
        "variables": {
            "exclusive_action": {
                "query": "What actions do people take, or what do they give up, to improve themselves or feel more in control?",
                "type": "A single decisive action verb phrase (one action only).",
                "must_not": [
                    "emotions",
                    "explanations or reasons",
                    "multiple actions",
                    "narrative or commentary",
                    "multi-sentence content"
                ]
            },
            "this_unique_thing": {
                "query": "What unique benefit, feeling, or transformation do people experience after making that change?",
                "type": "A short noun phrase describing a concrete benefit or transformation.",
                "must_not": [
                    "full sentences",
                    "story fragments",
                    "emotional confession",
                    "psychological analysis"
                ]
            }
        }
    },
    {
        "name": "Emotional Reversal",
        "template": "This {unexpectedly_changed_my_feelings} about {neutral_activity}.",
        "description": "Shows emotional transformation and intrinsic motivation.",
        "variables": {
            "unexpectedly_changed_my_feelings": {
                "query": "What experiences or realizations changed how people feel, often in a surprising or positive way?",
                "type": "A concise emotional shift or realization (noun phrase).",
                "must_not": [
                    "multiple clauses",
                    "behavioral descriptions",
                    "storytelling",
                    "explanations"
                ]
            },
            "neutral_activity": {
                "query": "What ordinary, neutral, or routine activity were people talking about before it became meaningful?",
                "type": "A neutral, everyday activity (noun phrase).",
                "must_not": [
                    "emotions",
                    "value judgments",
                    "psychological states",
                    "extended descriptions"
                ]
            }
        }
    },
    {
        "name": "Collective Mastery",
        "template": "{ingroup} figured out how to {aspirational_outcome} in {timeframe}.",
        "description": "Highlights group identity, pride, and future orientation.",
        "variables": {
            "ingroup": {
                "query": "What group, community, or type of people are mentioned as working together or sharing values?",
                "type": "A group identity noun phrase.",
                "must_not": [
                    "specific individuals",
                    "emotional language",
                    "descriptions longer than one phrase"
                ]
            },
            "aspirational_outcome": {
                "query": "What meaningful goal or improvement did people achieve or learn?",
                "type": "A short verb phrase describing a positive outcome.",
                "must_not": [
                    "reasons",
                    "explanations",
                    "long clauses"
                ]
            },
            "timeframe": {
                "query": "In what period or pace did this progress occur?",
                "type": "A concise timeframe (number + unit).",
                "must_not": [
                    "metaphorical time",
                    "vague or poetic phrasing",
                    "extended descriptions"
                ]
            }
        }
    },
    {
        "name": "Underdog Authority",
        "template": "The {unexpected_person} who {did_surprising_thing} is {intriguing_outcome}.",
        "description": "Uses status violation and admiration to evoke curiosity.",
        "variables": {
            "unexpected_person": {
                "query": "Who is an underestimated or ordinary person who achieved something notable?",
                "type": "A simple noun phrase referring to a person or role.",
                "must_not": [
                    "descriptions",
                    "emotions",
                    "multiple people"
                ]
            },
            "did_surprising_thing": {
                "query": "What surprising or counterintuitive thing did this person do?",
                "type": "A concise verb phrase.",
                "must_not": [
                    "explanations",
                    "multi-step actions",
                    "emotional language"
                ]
            },
            "intriguing_outcome": {
                "query": "What interesting result came from their action?",
                "type": "A short outcome phrase (adjective + noun OR verb phrase).",
                "must_not": [
                    "stories",
                    "full sentences",
                    "emotional framing"
                ]
            }
        }
    },
    {
        "name": "Relational Praise Curiosity",
        "template": "{close_figure} said I'm finally {positive_outcome} — and here's why.",
        "description": "Uses relational validation and a curiosity gap to highlight personal growth.",
        "variables": {
            "close_figure": {
                "query": "What close social figure offers emotionally resonant validation?",
                "type": "A social relationship role (noun phrase).",
                "must_not": [
                    "descriptions",
                    "emotions",
                    "multiple figures"
                ]
            },
            "positive_outcome": {
                "query": "What desirable transformation or identity improvement was noticed?",
                "type": "A short positive outcome (adjective + noun OR verb phrase).",
                "must_not": [
                    "explanations",
                    "full sentences",
                    "emotion words"
                ]
            }
        }
    },
    {
        "name": "Peer Recognition Mystery",
        "template": "{peer_group} keep asking why I'm suddenly {new_social_trait}.",
        "description": "Uses implicit social proof and status elevation.",
        "variables": {
            "peer_group": {
                "query": "Which social circle is noticing the change?",
                "type": "A plural group noun phrase.",
                "must_not": [
                    "emotions",
                    "descriptions",
                    "specific individuals"
                ]
            },
            "new_social_trait": {
                "query": "What desirable trait is being noticed?",
                "type": "A short trait phrase (adjective + noun OR behavioral noun).",
                "must_not": [
                    "explanations",
                    "multi-clause descriptions",
                    "emotional states"
                ]
            }
        }
    },
    {
        "name": "Rapid Breakthrough Liberation",
        "template": "I {quit_negative_habit} after {short_timeframe} of using this — and I don’t even miss it.",
        "description": "Frames a counter-normative decision as easy and liberating.",
        "variables": {
            "quit_negative_habit": {
                "query": "What hard-to-quit behavior did someone give up?",
                "type": "A negative behavior or habit (noun phrase).",
                "must_not": [
                    "emotions",
                    "motives",
                    "psychological analysis",
                    "full clauses"
                ]
            },
            "short_timeframe": {
                "query": "What short timeframe implies rapid change?",
                "type": "A simple timeframe (number + unit).",
                "must_not": [
                    "vague timeframes",
                    "emotional descriptions",
                    "extended wording"
                ]
            }
        }
    },
    {
        "name": "Awakened Curiosity",
        "template": "How my {dormant_quality} finally came back to life.",
        "description": "Shows revival of a valued inner quality.",
        "variables": {
            "dormant_quality": {
                "query": "What inner quality felt lost or suppressed before returning?",
                "type": "A short noun phrase naming an internal quality.",
                "must_not": [
                    "actions",
                    "explanations",
                    "extended descriptors"
                ]
            }
        }
    },
    {
        "name": "Ex-Validation Twist",
        "template": "My ex said '{unexpected_comment}'...",
        "description": "Uses a surprising comment to create an identity contrast.",
        "variables": {
            "unexpected_comment": {
                "query": "What provocative or revealing statement did an ex make?",
                "type": "A short quoted sentence or phrase.",
                "must_not": [
                    "multiple sentences",
                    "storytelling",
                    "explanations",
                    "long emotional descriptions"
                ]
            }
        }
    },
    {
        "name": "Regretful Insight",
        "template": "Wish someone told me about {this_insight} when I was {wasting_time_behavior}.",
        "description": "Shows retrospective regret and transformation.",
        "variables": {
            "this_insight": {
                "query": "What powerful realization do people wish they discovered earlier?",
                "type": "A short noun phrase naming an insight, tactic, or tool.",
                "must_not": [
                    "stories",
                    "explanations",
                    "multiple clauses"
                ]
            },
            "wasting_time_behavior": {
                "query": "What unproductive habit consumed time before improvement?",
                "type": "A negative behavior or habit (noun phrase).",
                "must_not": [
                    "emotions",
                    "meta-analysis",
                    "sentence-level content"
                ]
            }
        }
    },
    {
        "name": "Empowered Intelligence",
        "template": "This teaches you to stop feeling bad about being {positive_trait}.",
        "description": "Reframes a stigmatized positive trait as empowerment.",
        "variables": {
            "positive_trait": {
                "query": "What undervalued trait should people feel proud of?",
                "type": "A positive trait or characteristic (adjective + noun OR noun phrase).",
                "must_not": [
                    "full sentences",
                    "emotions",
                    "extended descriptions"
                ]
            }
        }
    },
    {
        "name": "Curiosity-Friendship Non-Tradeoff",
        "template": "Apparently you don't have to sacrifice {personal_value} to keep {social_group}.",
        "description": "Challenges tradeoff assumptions between personal growth and social belonging.",
        "variables": {
            "personal_value": {
                "query": "What personal value or pursuit do people fear will distance them from others?",
                "type": "A short noun phrase naming a value or pursuit.",
                "must_not": [
                    "sentences",
                    "emotional language",
                    "explanations"
                ]
            },
            "social_group": {
                "query": "What group of friends do people want to stay connected to?",
                "type": "A plural group noun phrase.",
                "must_not": [
                    "emotions",
                    "descriptions",
                    "proper names"
                ]
            }
        }
    },
    {
        "name": "Consequential Authority Reveal",
        "template": "This is what happens when {unusual_agent} finally gets {new_capability}.",
        "description": "Frames a surprising or status-violating outcome caused by giving agency or tools to an unexpected group.",
        "variables": {
            "unusual_agent": {
                "query": "Who is an unexpected or underestimated group gaining influence or tools?",
                "type": "A plural or collective noun phrase.",
                "must_not": [
                    "emotional labels",
                    "descriptions",
                    "specific individuals"
                ]
            },
            "new_capability": {
                "query": "What new ability or leverage did they receive?",
                "type": "A short noun phrase.",
                "must_not": [
                    "explanations",
                    "clauses",
                    "emotions"
                ]
            }
        }
    },
    {
        "name": "Anti-Conformity Shield",
        "template": "Found something that stops you from {forced_social_compromise}.",
        "description": "Positions the product as protection against social conformity pressure.",
        "variables": {
            "forced_social_compromise": {
                "query": "What behavior do people feel pressured into to fit in?",
                "type": "A negative behavioral noun phrase.",
                "must_not": [
                    "multiple behaviors",
                    "emotional framing",
                    "long clauses"
                ]
            }
        }
    },
    {
        "name": "Unmasked Competence",
        "template": "This showed me how to stop {self_silencing_behavior}.",
        "description": "Describes the removal of self-suppression around intelligence or competence.",
        "variables": {
            "self_silencing_behavior": {
                "query": "What competence-hiding behavior were people doing before?",
                "type": "A short verb phrase or behavioral noun phrase.",
                "must_not": [
                    "explanations",
                    "emotional confession",
                    "multiple behaviors"
                ]
            }
        }
    },
    {
        "name": "Social Permission Reframe",
        "template": "Apparently it's fine to {previously_discouraged_strength} sometimes.",
        "description": "Normalizes a trait or behavior people felt socially punished for.",
        "variables": {
            "previously_discouraged_strength": {
                "query": "What positive trait or behavior did people feel they had to downplay?",
                "type": "A short verb phrase or trait phrase.",
                "must_not": [
                    "emotional states",
                    "multi-clause content",
                    "explanations"
                ]
            }
        }
    },
    {
        "name": "Intuitive Learning Compass",
        "template": "This teaches you to trust your gut about {learning_focus}.",
        "description": "Highlights regained intuition for deciding what knowledge is valuable.",
        "variables": {
            "learning_focus": {
                "query": "What area of learning or information choice must people rely on intuition for?",
                "type": "A short noun phrase.",
                "must_not": [
                    "sentences",
                    "emotional language",
                    "explanations"
                ]
            }
        }
    },
    {
        "name": "Prescriptive Wisdom Signal",
        "template": "Honestly, this should be required for {target_group}.",
        "description": "Acts as a normative endorsement suggesting the product benefits an entire demographic.",
        "variables": {
            "target_group": {
                "query": "What demographic, age group, or category of people is being advised?",
                "type": "A plural or category noun phrase.",
                "must_not": [
                    "descriptions",
                    "emotional qualifiers",
                    "proper names"
                ]
            }
        }
    },
    {
        "name": "Forbidden Insight Tease",
        "template": "{suppressing_group} don’t want you to know about {restricted_insight}.",
        "description": "Creates curiosity through a sense of suppressed or hidden knowledge by a group with influence or status.",
        "variables": {
            "suppressing_group": {
                "query": "Which group or authority is implied to be hiding information?",
                "type": "A plural noun phrase referring to a group.",
                "must_not": [
                    "specific individuals",
                    "emotional framing",
                    "long descriptions"
                ]
            },
            "restricted_insight": {
                "query": "What powerful or useful insight is supposedly being kept from people?",
                "type": "A short noun phrase.",
                "must_not": [
                    "explanations",
                    "multi-clause descriptions",
                    "emotional labels"
                ]
            }
        }
    },
    {
        "name": "Meta-Effectiveness Reveal",
        "template": "Plot twist: {unexpected_effectiveness}.",
        "description": "Subverts expectations by contrasting the product’s success with previous failures.",
        "variables": {
            "unexpected_effectiveness": {
                "query": "What concise phrase captures the surprising success compared to past disappointments?",
                "type": "A short noun or verb phrase expressing surprising effectiveness.",
                "must_not": [
                    "multiple sentences",
                    "storytelling",
                    "explicit comparisons",
                    "emotional confession"
                ]
            }
        }
    },
    {
        "name": "Independent Thought Activation",
        "template": "Found something that helps you {independent_thought_action}.",
        "description": "Highlights regaining the ability to think independently rather than following trends or external validation.",
        "variables": {
            "independent_thought_action": {
                "query": "What internal cognitive behavior did people regain or strengthen?",
                "type": "A short verb phrase representing autonomous thinking.",
                "must_not": [
                    "multi-step behavior",
                    "emotional descriptors",
                    "full clauses"
                ]
            }
        }
    }

]

SCHEMAS = [
    {
        "name": "Proven Video Script Core Block",
        "template": "Instead of {pain_topic_alternative}, you can use it to discover KEY IDEAS from {idea_source_list}. \n It only takes {time_value_claim}. \n It's super intuitive and you'll feel so much {intuitive_benefit}.",
        "description": "Preserves the exact line structure and flow of the successful script lines (00:04-00:18) while injecting fresh language via variables.",
        "variables": {
            "pain_topic_alternative": {
                "query": "What frustrating, time-wasting behavior are people stuck in?",
                "type": "A descriptive noun phrase for a negative state (e.g., 'endless brain rot', 'mindless scrolling').",
                "must_not": [
                    "full sentences",
                    "solutions or products",
                    "the word 'doomscrolling'"
                ]
            },
            # "idea_source_list": {
            #     "query": "What high-status sources of ideas do people want quick access to?",
            #     "type": "A short, comma-separated list of high-status idea sources or topics.",
            #     "must_not": [
            #         "full sentences",
            #         "explanations"
            #     ]
            # },
            # "time_value_claim": {
            #     "query": "What short time-for-value promise would feel impressive to them?",
            #     "type": "A short phrase combining a specific duration with a high-value outcome.",
            #     "must_not": [
            #         "vague claims",
            #         "full sentences",
            #         "emotional confessions"
            #     ]
            # },
            "intuitive_benefit": {
                "query": "What modern, aspirational outcome do people want more of?",
                "type": "A short, modern, aspirational outcome phrase.",
                "must_not": [
                    "vague feelings (happy, good)",
                    "story fragments",
                    "full sentences"
                ]
            }
        }
    },
]

def extract_schema_variables(comments, schema, topic, model="gpt-4.1"):
    system_prompt = f"""
You receive:
- a list of comments,
- a schema with variable names and rules,
- a TOPIC.

TOPIC: "{topic}"

Extract ALL valid values for each schema variable using ONLY words from the comments.
Follow each variable’s type and must_not rules.

Output JSON ONLY in this exact format:

{{
  "var1": [...],
  "var2": [...]
}}

Rules:
- One array per variable.
- No schema-name keys.
- No objects, no pairs, no nesting.
- No paraphrasing.
- Empty array if nothing is found.
"""

    user_payload = {
        "comments": comments,
        "schema": schema
    }

    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": json.dumps(user_payload, ensure_ascii=False)}
        ],
        response_format={"type": "json_object"}
    )

    return json.loads(response.choices[0].message.content)


def run_extraction_pipeline(topic):
    topic_suffix = topic.replace(" ", "_").lower()

    for schema in SCHEMAS:
        schema_name = schema["name"].replace(" ", "_").lower()
        print(f"\n🌱 Schema: {schema['name']}")

        for var_name in schema["variables"].keys():
            var_safe = var_name.replace(" ", "_").lower()
            input_path = f"seeds-audible-reviews/{topic_suffix}_{schema_name}_{var_safe}.txt"

            if not (os.path.exists(input_path) and os.path.getsize(input_path) > 5):
                continue

            with open(input_path, "r", encoding="utf-8") as f:
                comments = f.read().split("\n")
                comments = [c.strip() for c in comments if c.strip()]

            print("🔍 Calling model…")

            result = extract_schema_variables(comments, schema, topic)

            values = result.get(var_name, [])

            output_path = f"seeds-audible-reviews/{topic_suffix}_{schema_name}_{var_safe}_keys.txt"

            with open(output_path, "w", encoding="utf-8") as f:
                if isinstance(values, list):
                    for v in values:
                        f.write(v.strip() + "\n")
                elif isinstance(values, str):
                    f.write(values.strip() + "\n")            

            print(f"✅ Saved {var_name} → {output_path}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Extract schema variables safely.")

    parser.add_argument(
        "--topic",
        type=str,
        required=True,
        help="Topic used to find comments and generate contextual extraction."
    )

    args = parser.parse_args()
    run_extraction_pipeline(args.topic)
