import json
from pathlib import Path
from entry_portal_generator import generate_nano_banana_portal

def generate_outreach_payload(business_name: str, industry: str, contact_email: str) -> dict:
    """
    Combines the dynamic Nano-Banana portal image with the cold email payload.
    This creates the final artifact ready to be pushed to an email sending platform (like GHL/Instantly).
    """
    print(f"[Outreach] Building payload for {business_name} ({industry})...")
    
    # 1. Generate the Nano-Banana image hook
    image_path = generate_nano_banana_portal(business_name, industry)
    
    if not image_path:
        print(f"[Outreach] Failed to generate image for {business_name}. Skipping payload.")
        return {}
        
    # In a real deployment, the image would be uploaded to an S3 bucket or CDN here to get a public URL.
    # For now, we simulate the CDN URL.
    safe_name = "".join([c for c in business_name if c.isalpha() or c.isdigit()]).lower()
    public_image_url = f"https://cdn.agileadapt.ai/hooks/{safe_name}_{industry}_payload.jpg"
    
    # 2. Construct the HTML Email Payload
    email_subject = f"We built a next-generation AI website for {business_name}"
    
    email_html = f"""
    <html>
        <body style="font-family: Arial, sans-serif; color: #333;">
            <p>Hi team at {business_name},</p>
            <p>I noticed your current website could use an upgrade, so my team ahead and built a high-fidelity, interactive prototype specifically for you.</p>
            <p>It includes a built-in AI Voice Receptionist to capture leads 24/7.</p>
            <p><strong>Here is a sneak peek:</strong></p>
            <a href="https://agileadapt.ai/demo/{safe_name}">
                <img src="{public_image_url}" alt="Your new website" style="max-width: 600px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);" />
            </a>
            <p>Click the image above to interact with the live demo we've hosted for you.</p>
            <p>If you'd like to claim this design and deploy it to your domain, let me know when you have 5 minutes to chat.</p>
            <p>Best,<br>The AgileAdapt Team</p>
        </body>
    </html>
    """
    
    payload = {
        "business_name": business_name,
        "contact_email": contact_email,
        "subject": email_subject,
        "html_body": email_html,
        "local_image_path": str(image_path),
        "public_image_url": public_image_url
    }
    
    print(f"[Outreach] Payload successfully generated for {contact_email}")
    return payload

if __name__ == "__main__":
    test_leads = [
        {"name": "Apex Legal Group", "industry": "legal", "email": "contact@apexlegal.example.com"},
        {"name": "Sunshine Roofing", "industry": "roofing", "email": "info@sunshineroofing.example.com"}
    ]
    
    results = []
    for lead in test_leads:
        res = generate_outreach_payload(lead["name"], lead["industry"], lead["email"])
        if res:
            results.append(res)
            
    # Save the payloads to a JSON file for the email sender script to consume
    output_path = Path(__file__).parent.parent / "Sunaiva" / "website_factory" / "outreach_payloads.json"
    with open(output_path, "w") as f:
        json.dump(results, f, indent=4)
        
    print(f"\\nAll payloads saved to {output_path}")
