from django.db import migrations


def create_default_investment_plans(apps, schema_editor):
    InvestmentPlan = apps.get_model("core", "InvestmentPlan")

    plans = [
        {
            "name": "Plan A",
            "description": "Invest $50 and earn 20% after 7 days.",
            "amount_cents": 5000,
            "currency": "USD",
            "duration_days": 7,
            "profit_percent": 20.00,
            "is_active": True,
        },
        {
            "name": "Plan B",
            "description": "Invest $200 and earn 40% after 14 days.",
            "amount_cents": 20000,
            "currency": "USD",
            "duration_days": 14,
            "profit_percent": 40.00,
            "is_active": True,
        },
        {
            "name": "Plan C",
            "description": "Invest $500 and earn 80% after 30 days.",
            "amount_cents": 50000,
            "currency": "USD",
            "duration_days": 30,
            "profit_percent": 80.00,
            "is_active": True,
        },
    ]

    for plan_data in plans:
        InvestmentPlan.objects.update_or_create(
            name=plan_data["name"],
            defaults={
                "description": plan_data["description"],
                "amount_cents": plan_data["amount_cents"],
                "currency": plan_data["currency"],
                "duration_days": plan_data["duration_days"],
                "profit_percent": plan_data["profit_percent"],
                "is_active": plan_data["is_active"],
            },
        )


def reverse_default_investment_plans(apps, schema_editor):
    InvestmentPlan = apps.get_model("core", "InvestmentPlan")
    InvestmentPlan.objects.filter(name__in=["Plan A", "Plan B", "Plan C"]).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0007_investmentplan_investment_referralbonus_userwallet_and_more"),
    ]

    operations = [
        migrations.RunPython(create_default_investment_plans, reverse_default_investment_plans),
    ]
