import os
import json
import requests
from pathlib import Path

OPENROUTER_API_KEY = "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"

def generate_posts():
    prompt = """Generate 3 Facebook posts for each of the following brands based on their DNA:
1. Sunaiva (AI Memory & Knowledge Management). DNA: 'Living Memory', 'Bloodstream', 'Never Forget'.
2. AgileAdapt (AI Compliance & Strategy). DNA: 'The 9-Layer Shield', 'Compliance Insurance', 'Dec 2026 Privacy Act'.
3. Receptionist AI (Voice AI for Tradies). DNA: 'Never missing a lead', 'Natural Australian Voice', '24/7 Reception'.

The posts should be engaging, professional, and highlight the unique value proposition.
Return the results in a JSON format like this:
{
  "Sunaiva": ["post1", "post2", "post3"],
  "AgileAdapt": [...],
  "ReceptionistAI": [...]
}
Return ONLY the JSON."""

    headers = {
        "Authorization": f"Bearer {OPENROUTER_API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": "minimax/minimax-m2.5",
        "messages": [{"role": "user", "content": prompt}]
    }

    print("Generating FB posts via OpenRouter...")
    response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
    
    if response.status_code == 200:
        content = response.json()['choices'][0]['message']['content']
        # Extract JSON if there's markdown wrapping
        if "```json" in content:
            content = content.split("```json")[1].split("```")[0].strip()
        
        data = json.loads(content)
        output_path = Path("marketing/fb_content_batch_1.json")
        output_path.parent.mkdir(exist_ok=True)
        output_path.write_text(json.dumps(data, indent=2))
        print(f"Posts saved to {output_path}")
        return data
    else:
        print(f"Error: {response.status_code} - {response.text}")
        return None

if __name__ == "__main__":
    generate_posts()
