"""add main platform for marketing site

Revision ID: z6g7h8i9j0k1
Revises: z5f6g7h8i9j0
Create Date: 2026-01-19 14:00:00.000000

This migration adds the 'main' platform for the main marketing site:
1. Inserts main platform record (wizamart.lu)
2. Creates platform marketing pages (home, about, faq, pricing, contact)

The 'main' platform serves as the marketing homepage at:
- Development: localhost:9999/ (no /platforms/ prefix)
- Production: wizamart.lu/

All other platforms are accessed via:
- Development: localhost:9999/platforms/{code}/
- Production: {code}.lu or custom domain
"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "z6g7h8i9j0k1"
down_revision: str | None = "z5f6g7h8i9j0"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    conn = op.get_bind()

    # =========================================================================
    # 1. Insert Main Marketing platform
    # =========================================================================
    conn.execute(
        sa.text("""
            INSERT INTO platforms (code, name, description, domain, path_prefix, default_language,
                                   supported_languages, is_active, is_public, theme_config, settings,
                                   created_at, updated_at)
            VALUES ('main', 'Wizamart', 'Main marketing site showcasing all Wizamart platforms',
                    'wizamart.lu', NULL, 'fr', '["fr", "de", "en"]', true, true,
                    '{"primary_color": "#2563EB", "secondary_color": "#3B82F6"}',
                    '{"is_marketing_site": true}',
                    CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
        """)
    )

    # Get the Main platform ID
    result = conn.execute(sa.text("SELECT id FROM platforms WHERE code = 'main'"))
    main_platform_id = result.fetchone()[0]

    # =========================================================================
    # 2. Create platform marketing pages (is_platform_page=True)
    # =========================================================================
    platform_pages = [
        {
            "slug": "home",
            "title": "Wizamart - E-commerce Solutions for Luxembourg",
            "content": """<div class="hero-section">
    <h1>Build Your Business with Wizamart</h1>
    <p class="lead">All-in-one e-commerce, loyalty, and business solutions for Luxembourg merchants</p>
</div>

<div class="platforms-showcase">
    <h2>Our Platforms</h2>

    <div class="platform-card">
        <h3>Wizamart OMS</h3>
        <p>Order Management System for multi-channel selling. Manage orders, inventory, and fulfillment from one dashboard.</p>
        <a href="/platforms/oms/" class="btn">Learn More</a>
    </div>

    <div class="platform-card">
        <h3>Loyalty+</h3>
        <p>Customer loyalty platform to reward your customers and increase retention. Points, rewards, and member tiers.</p>
        <a href="/platforms/loyalty/" class="btn">Learn More</a>
    </div>

    <div class="platform-card">
        <h3>Site Builder</h3>
        <p>Create beautiful websites for your local business. No coding required.</p>
        <span class="badge">Coming Soon</span>
    </div>
</div>

<div class="why-wizamart">
    <h2>Why Choose Wizamart?</h2>
    <ul>
        <li><strong>Made for Luxembourg:</strong> Built specifically for Luxembourg businesses with local payment methods, languages, and compliance.</li>
        <li><strong>All-in-One:</strong> Use our platforms together or separately - they integrate seamlessly.</li>
        <li><strong>Local Support:</strong> Real support from real people in Luxembourg.</li>
    </ul>
</div>""",
            "meta_description": "Wizamart offers e-commerce, loyalty, and business solutions for Luxembourg merchants. OMS, Loyalty+, and Site Builder platforms.",
            "show_in_header": False,
            "show_in_footer": False,
            "display_order": 0,
        },
        {
            "slug": "about",
            "title": "About Wizamart",
            "content": """<div class="about-page">
    <h1>About Wizamart</h1>

    <div class="mission">
        <h2>Our Mission</h2>
        <p>We're building the tools Luxembourg businesses need to thrive in the digital economy. From order management to customer loyalty, we provide the infrastructure that powers local commerce.</p>
    </div>

    <div class="story">
        <h2>Our Story</h2>
        <p>Wizamart was founded with a simple idea: Luxembourg businesses deserve world-class e-commerce tools that understand their unique needs. Local languages, local payment methods, local compliance - built in from the start.</p>
    </div>

    <div class="team">
        <h2>Our Team</h2>
        <p>We're a team of developers, designers, and business experts based in Luxembourg. We understand the local market because we're part of it.</p>
    </div>

    <div class="values">
        <h2>Our Values</h2>
        <ul>
            <li><strong>Simplicity:</strong> Powerful tools that are easy to use</li>
            <li><strong>Reliability:</strong> Your business depends on us - we take that seriously</li>
            <li><strong>Local First:</strong> Built for Luxembourg, by Luxembourg</li>
            <li><strong>Innovation:</strong> Always improving, always evolving</li>
        </ul>
    </div>
</div>""",
            "meta_description": "Learn about Wizamart, the Luxembourg-based company building e-commerce and business solutions for local merchants.",
            "show_in_header": True,
            "show_in_footer": True,
            "display_order": 1,
        },
        {
            "slug": "pricing",
            "title": "Pricing - Wizamart",
            "content": """<div class="pricing-page">
    <h1>Choose Your Platform</h1>
    <p class="lead">Each platform has its own pricing. Choose the tools your business needs.</p>

    <div class="platform-pricing">
        <div class="platform-pricing-card">
            <h3>Wizamart OMS</h3>
            <p>Order Management System</p>
            <div class="price-range">From €49/month</div>
            <ul>
                <li>Multi-channel order management</li>
                <li>Inventory tracking</li>
                <li>Shipping integrations</li>
                <li>Analytics dashboard</li>
            </ul>
            <a href="/platforms/oms/pricing" class="btn">View OMS Pricing</a>
        </div>

        <div class="platform-pricing-card">
            <h3>Loyalty+</h3>
            <p>Customer Loyalty Platform</p>
            <div class="price-range">From €49/month</div>
            <ul>
                <li>Points & rewards system</li>
                <li>Member tiers</li>
                <li>Analytics & insights</li>
                <li>POS integrations</li>
            </ul>
            <a href="/platforms/loyalty/pricing" class="btn">View Loyalty+ Pricing</a>
        </div>

        <div class="platform-pricing-card">
            <h3>Bundle & Save</h3>
            <p>Use multiple platforms together</p>
            <div class="price-range">Save up to 20%</div>
            <ul>
                <li>Seamless integration</li>
                <li>Unified dashboard</li>
                <li>Single invoice</li>
                <li>Priority support</li>
            </ul>
            <a href="/contact" class="btn">Contact Sales</a>
        </div>
    </div>
</div>""",
            "meta_description": "Wizamart pricing for OMS, Loyalty+, and bundled solutions. Plans starting at €49/month.",
            "show_in_header": True,
            "show_in_footer": True,
            "display_order": 2,
        },
        {
            "slug": "faq",
            "title": "FAQ - Frequently Asked Questions",
            "content": """<div class="faq-page">
    <h1>Frequently Asked Questions</h1>

    <div class="faq-section">
        <h2>General</h2>

        <div class="faq-item">
            <h3>What is Wizamart?</h3>
            <p>Wizamart is a suite of business tools for Luxembourg merchants, including order management (OMS), customer loyalty (Loyalty+), and website building (Site Builder).</p>
        </div>

        <div class="faq-item">
            <h3>Do I need to use all platforms?</h3>
            <p>No! Each platform works independently. Use one, two, or all three - whatever fits your business needs.</p>
        </div>

        <div class="faq-item">
            <h3>What languages are supported?</h3>
            <p>All platforms support French, German, and English - the three main languages of Luxembourg.</p>
        </div>
    </div>

    <div class="faq-section">
        <h2>Billing & Pricing</h2>

        <div class="faq-item">
            <h3>Is there a free trial?</h3>
            <p>Yes! All platforms offer a 14-day free trial with no credit card required.</p>
        </div>

        <div class="faq-item">
            <h3>What payment methods do you accept?</h3>
            <p>We accept credit cards, SEPA direct debit, and bank transfers.</p>
        </div>

        <div class="faq-item">
            <h3>Can I cancel anytime?</h3>
            <p>Yes, you can cancel your subscription at any time. No long-term contracts required.</p>
        </div>
    </div>

    <div class="faq-section">
        <h2>Support</h2>

        <div class="faq-item">
            <h3>How do I get help?</h3>
            <p>All plans include email support. Professional and Business plans include priority support with faster response times.</p>
        </div>

        <div class="faq-item">
            <h3>Do you offer onboarding?</h3>
            <p>Yes! We offer guided onboarding for all new customers to help you get started quickly.</p>
        </div>
    </div>
</div>""",
            "meta_description": "Frequently asked questions about Wizamart platforms, pricing, billing, and support.",
            "show_in_header": True,
            "show_in_footer": True,
            "display_order": 3,
        },
        {
            "slug": "contact",
            "title": "Contact Us - Wizamart",
            "content": """<div class="contact-page">
    <h1>Contact Us</h1>
    <p class="lead">We'd love to hear from you. Get in touch with our team.</p>

    <div class="contact-options">
        <div class="contact-card">
            <h3>Sales</h3>
            <p>Interested in our platforms? Let's talk about how we can help your business.</p>
            <p><a href="mailto:sales@wizamart.lu">sales@wizamart.lu</a></p>
        </div>

        <div class="contact-card">
            <h3>Support</h3>
            <p>Already a customer? Our support team is here to help.</p>
            <p><a href="mailto:support@wizamart.lu">support@wizamart.lu</a></p>
        </div>

        <div class="contact-card">
            <h3>General Inquiries</h3>
            <p>For everything else, reach out to our general inbox.</p>
            <p><a href="mailto:hello@wizamart.lu">hello@wizamart.lu</a></p>
        </div>
    </div>

    <div class="address">
        <h3>Office</h3>
        <p>Wizamart S.à r.l.<br>
        Luxembourg City<br>
        Luxembourg</p>
    </div>
</div>""",
            "meta_description": "Contact Wizamart for sales, support, or general inquiries. We're here to help your Luxembourg business succeed.",
            "show_in_header": True,
            "show_in_footer": True,
            "display_order": 4,
        },
        {
            "slug": "terms",
            "title": "Terms of Service - Wizamart",
            "content": """<div class="terms-page">
    <h1>Terms of Service</h1>
    <p class="last-updated">Last updated: January 2026</p>

    <p>These Terms of Service govern your use of Wizamart platforms and services.</p>

    <h2>1. Acceptance of Terms</h2>
    <p>By accessing or using our services, you agree to be bound by these Terms.</p>

    <h2>2. Services</h2>
    <p>Wizamart provides e-commerce and business management tools including order management, loyalty programs, and website building services.</p>

    <h2>3. Account Registration</h2>
    <p>You must provide accurate information when creating an account and keep your login credentials secure.</p>

    <h2>4. Fees and Payment</h2>
    <p>Subscription fees are billed in advance on a monthly or annual basis. Prices are listed in EUR and include applicable VAT for Luxembourg customers.</p>

    <h2>5. Data Protection</h2>
    <p>We process personal data in accordance with our Privacy Policy and applicable data protection laws including GDPR.</p>

    <h2>6. Limitation of Liability</h2>
    <p>Our liability is limited to the amount paid for services in the 12 months preceding any claim.</p>

    <h2>7. Governing Law</h2>
    <p>These Terms are governed by Luxembourg law. Disputes shall be resolved in Luxembourg courts.</p>

    <h2>8. Contact</h2>
    <p>For questions about these Terms, contact us at legal@wizamart.lu</p>
</div>""",
            "meta_description": "Wizamart Terms of Service. Read the terms and conditions for using our e-commerce and business platforms.",
            "show_in_header": False,
            "show_in_footer": True,
            "show_in_legal": True,
            "display_order": 10,
        },
        {
            "slug": "privacy",
            "title": "Privacy Policy - Wizamart",
            "content": """<div class="privacy-page">
    <h1>Privacy Policy</h1>
    <p class="last-updated">Last updated: January 2026</p>

    <h2>Introduction</h2>
    <p>Wizamart S.à r.l. ("we", "us") is committed to protecting your privacy. This policy explains how we collect, use, and protect your personal data.</p>

    <h2>Data Controller</h2>
    <p>Wizamart S.à r.l.<br>Luxembourg City, Luxembourg<br>Email: privacy@wizamart.lu</p>

    <h2>Data We Collect</h2>
    <ul>
        <li>Account information (name, email, company details)</li>
        <li>Usage data (how you use our platforms)</li>
        <li>Payment information (processed by our payment providers)</li>
        <li>Support communications</li>
    </ul>

    <h2>How We Use Your Data</h2>
    <ul>
        <li>To provide and improve our services</li>
        <li>To process payments and billing</li>
        <li>To communicate with you about your account</li>
        <li>To send marketing communications (with your consent)</li>
    </ul>

    <h2>Your Rights</h2>
    <p>Under GDPR, you have the right to:</p>
    <ul>
        <li>Access your personal data</li>
        <li>Rectify inaccurate data</li>
        <li>Request deletion of your data</li>
        <li>Data portability</li>
        <li>Object to processing</li>
    </ul>

    <h2>Contact</h2>
    <p>To exercise your rights or ask questions, contact our Data Protection Officer at privacy@wizamart.lu</p>
</div>""",
            "meta_description": "Wizamart Privacy Policy. Learn how we collect, use, and protect your personal data in compliance with GDPR.",
            "show_in_header": False,
            "show_in_footer": True,
            "show_in_legal": True,
            "display_order": 11,
        },
    ]

    for page in platform_pages:
        show_in_legal = page.get("show_in_legal", False)
        conn.execute(
            sa.text("""
                INSERT INTO content_pages (platform_id, vendor_id, slug, title, content, content_format,
                                          meta_description, is_published, is_platform_page,
                                          show_in_header, show_in_footer, show_in_legal, display_order,
                                          created_at, updated_at)
                VALUES (:platform_id, NULL, :slug, :title, :content, 'html',
                        :meta_description, true, true,
                        :show_in_header, :show_in_footer, :show_in_legal, :display_order,
                        CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
            """),
            {
                "platform_id": main_platform_id,
                "slug": page["slug"],
                "title": page["title"],
                "content": page["content"],
                "meta_description": page["meta_description"],
                "show_in_header": page["show_in_header"],
                "show_in_footer": page["show_in_footer"],
                "show_in_legal": show_in_legal,
                "display_order": page["display_order"],
            }
        )


def downgrade() -> None:
    conn = op.get_bind()

    # Get the Main platform ID
    result = conn.execute(sa.text("SELECT id FROM platforms WHERE code = 'main'"))
    row = result.fetchone()
    if row:
        main_platform_id = row[0]

        # Delete all content pages for main platform
        conn.execute(
            sa.text("DELETE FROM content_pages WHERE platform_id = :platform_id"),
            {"platform_id": main_platform_id}
        )

        # Delete vendor_platforms entries for main (if any)
        conn.execute(
            sa.text("DELETE FROM vendor_platforms WHERE platform_id = :platform_id"),
            {"platform_id": main_platform_id}
        )

    # Delete main platform
    conn.execute(sa.text("DELETE FROM platforms WHERE code = 'main'"))
