#!/usr/bin/env python
"""
Script to import/update the 3 plans (Starter, Growth, Scale) with the provided data.
"""
import os
import django
import sys

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings')
django.setup()

from igny8_core.auth.models import Plan
from decimal import Decimal

# Plan data from user
PLANS_DATA = [
    {
        "name": "Starter",
        "slug": "starter",
        "price": 89,
        "max_keywords": 500,
        "max_clusters": 100,
        "max_content_ideas": 300,
        "monthly_word_count_limit": 120000,
        "monthly_ai_credit_limit": 1000,
        "monthly_image_count": 120,
        "daily_content_tasks": 10,
        "daily_ai_request_limit": 50,
        "daily_image_generation_limit": 25,
        "included_credits": 1000,
        "extra_credit_price": 0.10,
        "max_sites": 3,
        "max_users": 5,
        "image_model_choices": ["hidream"],
        "features": ["ai_writer", "image_gen"]
    },
    {
        "name": "Growth",
        "slug": "growth",
        "price": 139,
        "max_keywords": 1000,
        "max_clusters": 200,
        "max_content_ideas": 600,
        "monthly_word_count_limit": 240000,
        "monthly_ai_credit_limit": 2000,
        "monthly_image_count": 240,
        "daily_content_tasks": 20,
        "daily_ai_request_limit": 100,
        "daily_image_generation_limit": 50,
        "included_credits": 2000,
        "extra_credit_price": 0.08,
        "max_sites": 10,
        "max_users": 10,
        "image_model_choices": ["dalle3", "hidream"],
        "features": ["ai_writer", "image_gen", "auto_publish"]
    },
    {
        "name": "Scale",
        "slug": "scale",
        "price": 229,
        "max_keywords": 2000,
        "max_clusters": 400,
        "max_content_ideas": 1200,
        "monthly_word_count_limit": 480000,
        "monthly_ai_credit_limit": 4000,
        "monthly_image_count": 500,
        "daily_content_tasks": 40,
        "daily_ai_request_limit": 200,
        "daily_image_generation_limit": 100,
        "included_credits": 4000,
        "extra_credit_price": 0.06,
        "max_sites": 25,
        "max_users": 25,
        "image_model_choices": ["dalle3", "hidream"],
        "features": ["ai_writer", "image_gen", "auto_publish", "custom_prompts"]
    }
]


def import_plans():
    """Import or update plans with the provided data."""
    print("Starting plan import/update...")
    
    for plan_data in PLANS_DATA:
        slug = plan_data['slug']
        
        # Convert price to Decimal
        plan_data['price'] = Decimal(str(plan_data['price']))
        plan_data['extra_credit_price'] = Decimal(str(plan_data['extra_credit_price']))
        
        # Get or create plan
        plan, created = Plan.objects.get_or_create(
            slug=slug,
            defaults=plan_data
        )
        
        if created:
            print(f"✅ Created new plan: {plan.name}")
        else:
            # Update existing plan
            print(f"🔄 Updating existing plan: {plan.name}")
            for key, value in plan_data.items():
                setattr(plan, key, value)
            plan.save()
            print(f"✅ Updated plan: {plan.name}")
        
        # Print plan details
        print(f"   - Price: ${plan.price}")
        print(f"   - Max Sites: {plan.max_sites}")
        print(f"   - Max Users: {plan.max_users}")
        print(f"   - Max Keywords: {plan.max_keywords}")
        print(f"   - Max Clusters: {plan.max_clusters}")
        print(f"   - Max Content Ideas: {plan.max_content_ideas}")
        print(f"   - Monthly AI Credits: {plan.monthly_ai_credit_limit}")
        print(f"   - Daily AI Request Limit: {plan.daily_ai_request_limit}")
        print(f"   - Daily Image Generation Limit: {plan.daily_image_generation_limit}")
        print(f"   - Features: {', '.join(plan.features)}")
        print()
    
    print("✅ Plan import/update completed successfully!")
    print(f"\nTotal plans in database: {Plan.objects.count()}")


if __name__ == '__main__':
    try:
        import_plans()
    except Exception as e:
        print(f"❌ Error importing plans: {e}", file=sys.stderr)
        import traceback
        traceback.print_exc()
        sys.exit(1)

