from django.db import migrations, models


PAGE_HEADERS = [
    {
        "key": "bots-hero", "page": "Bots", "section": "Hero",
        "eyebrow": "Marketplace", "title": "Trading Bot", "highlight_text": "Marketplace",
        "body": "Battle-proven automated strategies backtested over years of market data.",
    },
    {
        "key": "signals-hero", "page": "Signals", "section": "Hero",
        "eyebrow": "Real-Time Signals", "title": "Trade with", "highlight_text": "Confidence",
        "body": "Instant trading alerts from our proprietary analysis engine and professional traders.",
    },
    {
        "key": "pricing-hero", "page": "Pricing", "section": "Hero",
        "eyebrow": "💎 Pricing", "title": "Simple, Transparent", "highlight_text": "Pricing",
        "body": "Start free, upgrade when you're ready. No hidden fees, cancel anytime.",
    },
    {
        "key": "contact-hero", "page": "Contact", "section": "Hero",
        "eyebrow": "📧 Contact", "title": "Get in", "highlight_text": "Touch",
        "body": "Have a question, feedback, or partnership idea? We'd love to hear from you.",
    },
    {
        "key": "community-hero", "page": "Community", "section": "Hero",
        "eyebrow": "Trader Community", "title": "World-Class", "highlight_text": "Leaderboard",
        "body": "Connect with the top 1% of traders and follow their performance in real-time.",
    },
    {
        "key": "news-hero", "page": "News", "section": "Hero",
        "eyebrow": "Market Intelligence", "title": "Market", "highlight_text": "News",
        "body": "Real-time financial news, economic events, and market-moving headlines.",
    },
    {
        "key": "forex-hero", "page": "Forex", "section": "Hero",
        "eyebrow": "Forex Academy", "title": "Forex Academy", "highlight_text": "",
        "body": "Build a practical understanding of the forex market with step-by-step guided lessons.",
    },
    {
        "key": "crypto-hero", "page": "Crypto", "section": "Hero",
        "eyebrow": "Crypto Academy", "title": "Crypto Academy", "highlight_text": "",
        "body": "Learn how digital assets, exchanges, and on-chain narratives shape crypto trading.",
    },
    {
        "key": "analysis-header", "page": "Analysis", "section": "Header",
        "eyebrow": "Market Intelligence", "title": "Market Overview", "highlight_text": "",
        "body": "Live charts powered by TradingView — real-time price action.",
    },
]


NAVIGATION_ITEMS = [
    ("Home", "/", "header_primary", "", "home", 0),
    ("Dashboard", "/dashboard", "header_primary", "", "dashboard", 1),
    ("Marketplace", "/bots", "header_primary", "", "smart_toy", 2),
    ("Signals", "/signals", "header_primary", "", "sensors", 3),
    ("Pricing", "/pricing", "header_primary", "", "payments", 4),
    ("News", "/news", "header_primary", "", "newspaper", 5),
    ("Learn Forex", "/forex", "header_learn", "Forex trading course", "currency_exchange", 0),
    ("Learn Crypto", "/crypto", "header_learn", "Crypto & Web3 academy", "generating_tokens", 1),
    ("Community", "/community", "header_more", "Trader community", "groups", 0),
    ("Contact", "/contact", "header_more", "Get in touch", "mail", 1),
    ("Trading Bots", "/bots", "footer_platform", "", "", 0),
    ("Market Signals", "/signals", "footer_platform", "", "", 1),
    ("Analysis", "/analysis", "footer_platform", "", "", 2),
    ("News Feed", "/news", "footer_platform", "", "", 3),
    ("Forex Academy", "/forex", "footer_learn", "", "", 0),
    ("Crypto 101", "/crypto", "footer_learn", "", "", 1),
    ("Community", "/community", "footer_learn", "", "", 2),
    ("Contact Us", "/contact", "footer_learn", "", "", 3),
    ("Privacy Policy", "/privacy", "footer_legal", "", "", 0),
    ("Terms of Service", "/terms", "footer_legal", "", "", 1),
    ("Risk Disclosure", "/risk", "footer_legal", "", "", 2),
]


def seed_global_content(apps, schema_editor):
    ContentBlock = apps.get_model("base", "ContentBlock")
    SiteSettings = apps.get_model("base", "SiteSettings")
    NavigationItem = apps.get_model("base", "NavigationItem")

    SiteSettings.objects.get_or_create(
        key="main",
        defaults={
            "site_name": "TheStarFX",
            "site_tagline": "Premium Trading Platform",
            "footer_description": (
                "Premium Forex & Crypto education and automation platform. "
                "Empowering traders with battle-proven strategies and real-time data."
            ),
            "support_email": "support@thestarfx.com",
            "support_location": "Global Financial District",
            "copyright_text": "© 2026 TheStarFX Platform. All rights reserved.",
            "sign_in_label": "Sign In",
            "register_label": "Get Started",
            "learn_menu_label": "Learn",
            "more_menu_label": "More",
            "footer_platform_label": "Platform",
            "footer_learn_label": "Learn",
            "footer_contact_label": "Contact",
        },
    )

    for label, url, location, description, icon, order in NAVIGATION_ITEMS:
        NavigationItem.objects.get_or_create(
            label=label,
            url=url,
            location=location,
            defaults={
                "description": description,
                "icon": icon,
                "order": order,
                "is_active": True,
            },
        )

    for block in PAGE_HEADERS:
        key = block["key"]
        ContentBlock.objects.get_or_create(key=key, defaults={**block, "is_published": True})


def remove_global_content(apps, schema_editor):
    ContentBlock = apps.get_model("base", "ContentBlock")
    SiteSettings = apps.get_model("base", "SiteSettings")
    NavigationItem = apps.get_model("base", "NavigationItem")
    ContentBlock.objects.filter(key__in=[block["key"] for block in PAGE_HEADERS]).delete()
    NavigationItem.objects.all().delete()
    SiteSettings.objects.filter(key="main").delete()


class Migration(migrations.Migration):
    dependencies = [("base", "0002_contentblock")]

    operations = [
        migrations.CreateModel(
            name="SiteSettings",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("key", models.SlugField(default="main", max_length=50, unique=True)),
                ("site_name", models.CharField(default="TheStarFX", max_length=100)),
                ("site_tagline", models.CharField(blank=True, max_length=160)),
                ("logo_url", models.URLField(blank=True)),
                ("footer_description", models.TextField(blank=True)),
                ("support_email", models.EmailField(blank=True, max_length=254)),
                ("support_location", models.CharField(blank=True, max_length=160)),
                ("copyright_text", models.CharField(blank=True, max_length=255)),
                ("sign_in_label", models.CharField(default="Sign In", max_length=40)),
                ("register_label", models.CharField(default="Get Started", max_length=60)),
                ("learn_menu_label", models.CharField(default="Learn", max_length=40)),
                ("more_menu_label", models.CharField(default="More", max_length=40)),
                ("footer_platform_label", models.CharField(default="Platform", max_length=60)),
                ("footer_learn_label", models.CharField(default="Learn", max_length=60)),
                ("footer_contact_label", models.CharField(default="Contact", max_length=60)),
                ("telegram_url", models.URLField(blank=True)),
                ("discord_url", models.URLField(blank=True)),
                ("x_url", models.URLField(blank=True)),
                ("instagram_url", models.URLField(blank=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={"verbose_name_plural": "site settings"},
        ),
        migrations.CreateModel(
            name="NavigationItem",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("label", models.CharField(max_length=80)),
                ("url", models.CharField(max_length=255)),
                ("location", models.CharField(choices=[("header_primary", "Header — primary"), ("header_learn", "Header — Learn menu"), ("header_more", "Header — More menu"), ("footer_platform", "Footer — Platform"), ("footer_learn", "Footer — Learn"), ("footer_legal", "Footer — Legal")], db_index=True, max_length=30)),
                ("description", models.CharField(blank=True, max_length=140)),
                ("icon", models.CharField(blank=True, help_text="Material Symbols icon name", max_length=60)),
                ("order", models.PositiveIntegerField(db_index=True, default=0)),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                ("open_new_tab", models.BooleanField(default=False)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={"ordering": ["location", "order", "id"]},
        ),
        migrations.AddIndex(
            model_name="navigationitem",
            index=models.Index(fields=["location", "is_active", "order"], name="base_nav_location_active_ord"),
        ),
        migrations.RunPython(seed_global_content, remove_global_content),
    ]
