# tests/fixtures/content_page_fixtures.py
"""
Content page test fixtures.

Note: Fixtures should NOT use db.expunge() as it breaks lazy loading.
See tests/conftest.py for details on fixture best practices.
"""

import uuid

import pytest

from app.modules.cms.models import ContentPage


@pytest.fixture
def platform_about_page(db):
    """Create a platform-level About page (store_id=NULL)."""
    page = ContentPage(
        store_id=None,
        slug="about",
        title="About Us",
        content="<h1>About Our Platform</h1><p>Welcome to our platform.</p>",
        content_format="html",
        meta_description="Learn about our platform",
        is_published=True,
        show_in_footer=True,
        show_in_header=True,
        show_in_legal=False,
        display_order=1,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def platform_faq_page(db):
    """Create a platform-level FAQ page (store_id=NULL)."""
    page = ContentPage(
        store_id=None,
        slug="faq",
        title="Frequently Asked Questions",
        content="<h1>FAQ</h1><p>Common questions answered.</p>",
        content_format="html",
        meta_description="Frequently asked questions",
        is_published=True,
        show_in_footer=True,
        show_in_header=False,
        show_in_legal=False,
        display_order=2,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def platform_privacy_page(db):
    """Create a platform-level Privacy Policy page for legal section."""
    page = ContentPage(
        store_id=None,
        slug="privacy",
        title="Privacy Policy",
        content="<h1>Privacy Policy</h1><p>Your data is important to us.</p>",
        content_format="html",
        meta_description="Our privacy policy",
        is_published=True,
        show_in_footer=False,
        show_in_header=False,
        show_in_legal=True,
        display_order=1,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def platform_terms_page(db):
    """Create a platform-level Terms of Service page for legal section."""
    page = ContentPage(
        store_id=None,
        slug="terms",
        title="Terms of Service",
        content="<h1>Terms of Service</h1><p>By using our platform...</p>",
        content_format="html",
        meta_description="Our terms of service",
        is_published=True,
        show_in_footer=False,
        show_in_header=False,
        show_in_legal=True,
        display_order=2,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def platform_contact_page(db):
    """Create a platform-level Contact page."""
    page = ContentPage(
        store_id=None,
        slug="contact",
        title="Contact Us",
        content="<h1>Contact</h1><p>Get in touch.</p>",
        content_format="html",
        meta_description="Contact us",
        is_published=True,
        show_in_footer=True,
        show_in_header=True,
        show_in_legal=False,
        display_order=3,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def platform_draft_page(db):
    """Create an unpublished platform page (draft)."""
    page = ContentPage(
        store_id=None,
        slug="draft-page",
        title="Draft Page",
        content="<h1>Draft</h1><p>This is a draft.</p>",
        content_format="html",
        is_published=False,
        show_in_footer=True,
        show_in_header=False,
        show_in_legal=False,
        display_order=99,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def store_about_page(db, test_store):
    """Create a store-specific About page override."""
    page = ContentPage(
        store_id=test_store.id,
        slug="about",
        title="About Our Shop",
        content="<h1>About Our Shop</h1><p>Welcome to our shop.</p>",
        content_format="html",
        meta_description="Learn about our shop",
        is_published=True,
        show_in_footer=True,
        show_in_header=True,
        show_in_legal=False,
        display_order=1,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def store_shipping_page(db, test_store):
    """Create a store-specific Shipping page (no platform default)."""
    page = ContentPage(
        store_id=test_store.id,
        slug="shipping",
        title="Shipping Information",
        content="<h1>Shipping</h1><p>We ship to Luxembourg.</p>",
        content_format="html",
        meta_description="Shipping info",
        is_published=True,
        show_in_footer=True,
        show_in_header=False,
        show_in_legal=False,
        display_order=4,
    )
    db.add(page)
    db.commit()
    db.refresh(page)
    return page


@pytest.fixture
def all_platform_pages(
    db,
    platform_about_page,
    platform_faq_page,
    platform_privacy_page,
    platform_terms_page,
    platform_contact_page,
):
    """Create all platform pages for comprehensive testing."""
    return [
        platform_about_page,
        platform_faq_page,
        platform_privacy_page,
        platform_terms_page,
        platform_contact_page,
    ]


@pytest.fixture
def content_page_factory():
    """Factory function to create content pages in tests."""

    def _create_page(db, store_id=None, **kwargs):
        unique_id = str(uuid.uuid4())[:8]
        defaults = {
            "store_id": store_id,
            "slug": f"page-{unique_id}",
            "title": f"Test Page {unique_id}",
            "content": f"<p>Content for {unique_id}</p>",
            "content_format": "html",
            "is_published": True,
            "show_in_footer": True,
            "show_in_header": False,
            "show_in_legal": False,
            "display_order": 0,
        }
        defaults.update(kwargs)

        page = ContentPage(**defaults)
        db.add(page)
        db.commit()
        db.refresh(page)
        return page

    return _create_page
