# tests/fixtures/marketplace_product_fixtures.py
"""
Marketplace product test fixtures.

Note: Since title/description are now in translations table,
we create the translation alongside the marketplace product.

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.marketplace.models import (
    MarketplaceProduct,
    MarketplaceProductTranslation,
)


def _create_marketplace_product_with_translation(
    db,
    marketplace_product_id: str,
    title: str,
    description: str | None = None,
    **kwargs,
) -> MarketplaceProduct:
    """Helper to create a MarketplaceProduct with its English translation."""
    marketplace_product = MarketplaceProduct(
        marketplace_product_id=marketplace_product_id,
        **kwargs,
    )
    db.add(marketplace_product)
    db.flush()  # Get the ID

    # Create the English translation
    translation = MarketplaceProductTranslation(
        marketplace_product_id=marketplace_product.id,
        language="en",
        title=title,
        description=description,
    )
    db.add(translation)
    db.commit()
    db.refresh(marketplace_product)
    return marketplace_product


@pytest.fixture
def test_marketplace_product(db):
    """Create a test product with translation."""
    return _create_marketplace_product_with_translation(
        db,
        marketplace_product_id="TEST001",
        title="Test MarketplaceProduct",
        description="A test product",
        price="10.99",
        currency="EUR",
        brand="TestBrand",
        gtin="1234567890123",
        availability="in inventory",
        marketplace="Letzshop",
        store_name="TestStore",
    )


@pytest.fixture
def unique_product(db):
    """Create a unique product for tests that need isolated product data."""
    unique_id = str(uuid.uuid4())[:8]
    return _create_marketplace_product_with_translation(
        db,
        marketplace_product_id=f"UNIQUE_{unique_id}",
        title=f"Unique MarketplaceProduct {unique_id}",
        description=f"A unique test product {unique_id}",
        price="19.99",
        currency="EUR",
        brand=f"UniqueBrand_{unique_id}",
        gtin=f"123456789{unique_id[:4]}",
        availability="in inventory",
        marketplace="Letzshop",
        store_name=f"UniqueStore_{unique_id}",
        google_product_category=f"UniqueCategory_{unique_id}",
    )


@pytest.fixture
def multiple_products(db):
    """Create multiple products for testing statistics and pagination."""
    unique_id = str(uuid.uuid4())[:8]
    marketplace_products = []

    for i in range(5):
        marketplace_product = _create_marketplace_product_with_translation(
            db,
            marketplace_product_id=f"MULTI_{unique_id}_{i}",
            title=f"Multi MarketplaceProduct {i} {unique_id}",
            description=f"Multi test product {i}",
            price=f"{10 + i}.99",
            currency="EUR",
            brand=f"MultiBrand_{i % 3}",  # Create 3 different brands
            marketplace=f"MultiMarket_{i % 2}",  # Create 2 different marketplaces
            store_name=f"MultiStore_{i}",
            google_product_category=f"MultiCategory_{i % 2}",  # Create 2 different categories
            gtin=f"1234567890{i}{unique_id[:2]}",
        )
        marketplace_products.append(marketplace_product)

    return marketplace_products


def create_unique_marketplace_product_factory():
    """Factory function to create unique products in tests."""

    def _marketplace_create_product(db, title: str | None = None, **kwargs):
        unique_id = str(uuid.uuid4())[:8]
        defaults = {
            "marketplace_product_id": f"FACTORY_{unique_id}",
            "price": "15.99",
            "currency": "EUR",
            "marketplace": "TestMarket",
            "store_name": "TestStore",
        }
        defaults.update(kwargs)

        title = title or f"Factory MarketplaceProduct {unique_id}"

        return _create_marketplace_product_with_translation(
            db,
            title=title,
            **defaults,
        )

    return _marketplace_create_product


@pytest.fixture
def marketplace_product_factory():
    """Fixture that provides a product factory function"""
    return create_unique_marketplace_product_factory()


@pytest.fixture
def test_marketplace_product_with_inventory(
    db, test_marketplace_product, test_inventory
):
    """MarketplaceProduct with associated inventory record."""
    # Ensure they're linked by GTIN
    if test_marketplace_product.gtin != test_inventory.gtin:
        test_inventory.gtin = test_marketplace_product.gtin
        db.commit()
        db.refresh(test_inventory)

    return {
        "marketplace_product": test_marketplace_product,
        "inventory": test_inventory,
    }
