#!/usr/bin/env python3
"""
AIVA Social Warmup Generator
============================
Generates high-value, outcome-based social media content for the AgileAdapt business page.
Ensures the page looks professional and active before mass outreach begins.

Strategy: Nick Ponte x Frankie Finn
"""

import sys
import os
import json
from pathlib import Path
from datetime import datetime

# Add genesis path
GENESIS_ROOT = Path(__file__).parent.parent.parent
sys.path.insert(0, str(GENESIS_ROOT))

from core.gemini_executor import GeminiExecutor

class AIVASocial:
    def __init__(self):
        self.executor = GeminiExecutor(use_rate_maximizer=True)
        self.content_dir = GENESIS_ROOT / "revenue" / "data" / "social_warmup"
        self.content_dir.mkdir(exist_ok=True, parents=True)

    def generate_warmup_batch(self, count: int = 5):
        """Generates a batch of social media posts."""
        print(f"[AIVA SOCIAL] Generating {count} warmup posts for AgileAdapt...")
        
        system_instruction = """
        You are AIVA, the Social Architect for AgileAdapt.
        Your goal is to generate high-value social media posts (FB/Instagram) that establish authority in the "AI Voice Agent for Tradies/Influencers" niche.
        
        VOICE:
        - Professional yet regional (Sydney/Florida vibes).
        - Focus on 'Outcome' (Revenue, Time saved) over 'Feature' (AI, Tech).
        - Use the Frankie Finn 'Helpful Friend' persona.
        
        TOPICS:
        - Why every plumber needs an AI assistant for night calls.
        - The 'Shrewd Shoe Buyer' mindset (How to target high-intent clients).
        - Resolving 'AI Guilt' for business owners.
        - Case study teaser: How a Sydney Law firm automated their intake.
        
        FORMAT:
        Return a JSON list of posts:
        [
          {"title": "...", "caption": "...", "image_prompt": "Prompt for DALL-E/Genesis to create the visual", "platform": "Instagram/FB"}
        ]
        """
        
        prompt = f"Generate {count} unique, high-conversion social media posts for the 'Lead with Value' strategy."
        
        response = self.executor.execute_optimized(
            prompt=prompt,
            system_prompt=system_instruction,
            task_type="creative"
        )
        
        if response.success:
            # Robust JSON extraction
            content = response.response
            if "```json" in content:
                content = content.split("```json")[1].split("```")[0].strip()
            
            try:
                posts = json.loads(content)
                self._save_posts(posts)
                return posts
            except Exception as e:
                print(f"[ERROR] Failed to parse social posts: {e}")
                return []
        
        return []

    def _save_posts(self, posts):
        """Save posts to a timestamped file."""
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        output_file = self.content_dir / f"warmup_batch_{timestamp}.json"
        output_file.write_text(json.dumps(posts, indent=2))
        print(f"[AIVA SOCIAL] Saved {len(posts)} posts to {output_file}")

if __name__ == "__main__":
    social = AIVASocial()
    social.generate_warmup_batch(5)
