# tests/unit/models/database/test_marketplace_product.py
"""Unit tests for MarketplaceProduct database model."""
import pytest
from sqlalchemy.exc import IntegrityError

from models.database.marketplace_product import MarketplaceProduct


@pytest.mark.unit
@pytest.mark.database
class TestMarketplaceProductModel:
    """Test MarketplaceProduct model."""

    def test_marketplace_product_creation(self, db):
        """Test MarketplaceProduct model creation."""
        marketplace_product = MarketplaceProduct(
            marketplace_product_id="DB_TEST_001",
            title="Database Test Product",
            description="Testing product model",
            price="25.99",
            currency="USD",
            brand="DBTest",
            gtin="1234567890123",
            availability="in stock",
            marketplace="Letzshop",
            vendor_name="Test Vendor",
        )

        db.add(marketplace_product)
        db.commit()
        db.refresh(marketplace_product)

        assert marketplace_product.id is not None
        assert marketplace_product.marketplace_product_id == "DB_TEST_001"
        assert marketplace_product.title == "Database Test Product"
        assert marketplace_product.marketplace == "Letzshop"
        assert marketplace_product.created_at is not None

    def test_marketplace_product_id_uniqueness(self, db):
        """Test unique marketplace_product_id constraint."""
        product1 = MarketplaceProduct(
            marketplace_product_id="UNIQUE_001",
            title="Product 1",
            marketplace="Letzshop",
        )
        db.add(product1)
        db.commit()

        # Duplicate marketplace_product_id should raise error
        with pytest.raises(IntegrityError):
            product2 = MarketplaceProduct(
                marketplace_product_id="UNIQUE_001",
                title="Product 2",
                marketplace="Letzshop",
            )
            db.add(product2)
            db.commit()

    def test_marketplace_product_all_fields(self, db):
        """Test MarketplaceProduct with all optional fields."""
        marketplace_product = MarketplaceProduct(
            marketplace_product_id="FULL_001",
            title="Full Product",
            description="Complete product description",
            link="https://example.com/product",
            image_link="https://example.com/image.jpg",
            availability="in stock",
            price="99.99",
            brand="TestBrand",
            gtin="9876543210123",
            mpn="MPN123",
            condition="new",
            adult="no",
            age_group="adult",
            color="blue",
            gender="unisex",
            material="cotton",
            pattern="solid",
            size="M",
            google_product_category="Apparel & Accessories",
            product_type="Clothing",
            currency="EUR",
            marketplace="Letzshop",
            vendor_name="Full Vendor",
        )

        db.add(marketplace_product)
        db.commit()
        db.refresh(marketplace_product)

        assert marketplace_product.brand == "TestBrand"
        assert marketplace_product.gtin == "9876543210123"
        assert marketplace_product.color == "blue"
        assert marketplace_product.size == "M"

    def test_marketplace_product_custom_labels(self, db):
        """Test MarketplaceProduct with custom labels."""
        marketplace_product = MarketplaceProduct(
            marketplace_product_id="LABELS_001",
            title="Labeled Product",
            marketplace="Letzshop",
            custom_label_0="Label0",
            custom_label_1="Label1",
            custom_label_2="Label2",
            custom_label_3="Label3",
            custom_label_4="Label4",
        )

        db.add(marketplace_product)
        db.commit()
        db.refresh(marketplace_product)

        assert marketplace_product.custom_label_0 == "Label0"
        assert marketplace_product.custom_label_4 == "Label4"

    def test_marketplace_product_minimal_fields(self, db):
        """Test MarketplaceProduct with only required fields."""
        marketplace_product = MarketplaceProduct(
            marketplace_product_id="MINIMAL_001",
            title="Minimal Product",
        )

        db.add(marketplace_product)
        db.commit()
        db.refresh(marketplace_product)

        assert marketplace_product.id is not None
        assert marketplace_product.marketplace_product_id == "MINIMAL_001"
        assert marketplace_product.title == "Minimal Product"
        assert marketplace_product.description is None
        assert marketplace_product.price is None
