"""
Скрипт для загрузки демо тарифных планов
"""
from app.db.session import SessionLocal
from app.db.models import Plan
from decimal import Decimal


def load_plans():
    db = SessionLocal()
    
    try:
        # Проверяем, есть ли уже планы
        existing_plans = db.query(Plan).count()
        if existing_plans > 0:
            print(f"Тарифные планы уже существуют ({existing_plans} шт.)")
            return
        
        # Создаем тарифные планы
        plans = [
            Plan(
                name="Starter",
                description="Идеально для небольших проектов и стартапов",
                monthly_price=Decimal("500"),
                notifications_limit=1000,
                features={
                    "channels": ["email"],
                    "templates": 5,
                    "analytics": "basic",
                    "support": "email"
                },
                is_active=True
            ),
            Plan(
                name="Professional",
                description="Для растущих бизнесов с повышенными потребностями",
                monthly_price=Decimal("1500"),
                notifications_limit=10000,
                features={
                    "channels": ["email", "telegram", "vk"],
                    "templates": 20,
                    "analytics": "advanced",
                    "support": "priority",
                    "ab_testing": True,
                    "automation": True
                },
                is_active=True
            ),
            Plan(
                name="Business",
                description="Для крупных компаний с высокой нагрузкой",
                monthly_price=Decimal("5000"),
                notifications_limit=100000,
                features={
                    "channels": ["email", "telegram", "vk", "sms"],  # "whatsapp" закомментирован
                    "templates": "unlimited",
                    "analytics": "advanced",
                    "support": "24/7",
                    "ab_testing": True,
                    "automation": True,
                    "custom_integrations": True,
                    "dedicated_manager": True
                },
                is_active=True
            ),
            Plan(
                name="Enterprise",
                description="Индивидуальное решение для корпоративных клиентов",
                monthly_price=Decimal("15000"),
                notifications_limit=1000000,
                features={
                    "channels": "all",
                    "templates": "unlimited",
                    "analytics": "custom",
                    "support": "24/7 dedicated",
                    "ab_testing": True,
                    "automation": True,
                    "custom_integrations": True,
                    "dedicated_manager": True,
                    "sla": "99.99%",
                    "white_label": True
                },
                is_active=True
            ),
        ]
        
        for plan in plans:
            db.add(plan)
        
        db.commit()
        print(f"✅ Создано {len(plans)} тарифных планов")
        
        # Выводим созданные планы
        for plan in plans:
            print(f"  - {plan.name}: {plan.monthly_price}₽/мес, {plan.notifications_limit:,} уведомлений")
    
    except Exception as e:
        print(f"❌ Ошибка при создании планов: {e}")
        db.rollback()
    finally:
        db.close()


if __name__ == "__main__":
    load_plans()

