from django.db import migrations, models


def create_home_hero(apps, schema_editor):
    ContentBlock = apps.get_model("base", "ContentBlock")
    ContentBlock.objects.get_or_create(
        key="home-hero",
        defaults={
            "page": "Home",
            "section": "Hero",
            "eyebrow": "Live Markets Open — 24/5 Trading",
            "title": "Master the Markets.",
            "highlight_text": "Trade with Confidence.",
            "body": (
                "Professional-grade trading bots, real-time signals, and "
                "world-class education for the modern investor. Join 50,000+ "
                "traders worldwide."
            ),
            "primary_cta_label": "Start Learning Free",
            "primary_cta_url": "/pricing",
            "secondary_cta_label": "Explore Platform",
            "secondary_cta_url": "/dashboard",
            "content_json": {
                "stats": [
                    {"label": "Active Traders", "value": 50000, "suffix": "+"},
                    {"label": "Signal Accuracy", "value": 87, "suffix": "%", "decimals": 1},
                    {"label": "Video Lessons", "value": 200, "suffix": "+"},
                    {"label": "User Rating", "value": 4.9, "decimals": 1},
                ]
            },
            "is_published": True,
        },
    )


def remove_home_hero(apps, schema_editor):
    ContentBlock = apps.get_model("base", "ContentBlock")
    ContentBlock.objects.filter(key="home-hero").delete()


class Migration(migrations.Migration):
    dependencies = [("base", "0001_initial")]

    operations = [
        migrations.CreateModel(
            name="ContentBlock",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("key", models.SlugField(help_text="Stable identifier used by the frontend, for example: home-hero", max_length=100, unique=True)),
                ("page", models.CharField(db_index=True, max_length=50)),
                ("section", models.CharField(max_length=100)),
                ("eyebrow", models.CharField(blank=True, max_length=120)),
                ("title", models.CharField(max_length=255)),
                ("highlight_text", models.CharField(blank=True, max_length=255)),
                ("body", models.TextField(blank=True)),
                ("primary_cta_label", models.CharField(blank=True, max_length=80)),
                ("primary_cta_url", models.CharField(blank=True, max_length=255)),
                ("secondary_cta_label", models.CharField(blank=True, max_length=80)),
                ("secondary_cta_url", models.CharField(blank=True, max_length=255)),
                ("content_json", models.JSONField(blank=True, default=dict, help_text="Optional structured content such as statistics or feature lists.")),
                ("is_published", models.BooleanField(db_index=True, default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
            ],
            options={"ordering": ["page", "section"]},
        ),
        migrations.AddIndex(
            model_name="contentblock",
            index=models.Index(fields=["page", "is_published"], name="base_conten_page_b3d009_idx"),
        ),
        migrations.RunPython(create_home_hero, remove_home_hero),
    ]
