# tests/unit/models/database/test_product.py
"""Unit tests for Product (vendor catalog) database model."""
import pytest
from sqlalchemy.exc import IntegrityError

from models.database.product import Product


@pytest.mark.unit
@pytest.mark.database
class TestProductModel:
    """Test Product (vendor catalog) model."""

    def test_product_creation(self, db, test_vendor, test_marketplace_product):
        """Test Product model linking vendor catalog to marketplace product."""
        product = Product(
            vendor_id=test_vendor.id,
            marketplace_product_id=test_marketplace_product.id,
            product_id="VENDOR_PROD_001",
            price=89.99,
            currency="EUR",
            availability="in stock",
            is_featured=True,
            is_active=True,
        )

        db.add(product)
        db.commit()
        db.refresh(product)

        assert product.id is not None
        assert product.vendor_id == test_vendor.id
        assert product.marketplace_product_id == test_marketplace_product.id
        assert product.price == 89.99
        assert product.is_featured is True
        assert product.vendor.vendor_code == test_vendor.vendor_code
        assert product.marketplace_product.title == test_marketplace_product.title

    def test_product_unique_per_vendor(self, db, test_vendor, test_marketplace_product):
        """Test that same marketplace product can't be added twice to vendor catalog."""
        product1 = Product(
            vendor_id=test_vendor.id,
            marketplace_product_id=test_marketplace_product.id,
            is_active=True,
        )
        db.add(product1)
        db.commit()

        # Same marketplace product to same vendor should fail
        with pytest.raises(IntegrityError):
            product2 = Product(
                vendor_id=test_vendor.id,
                marketplace_product_id=test_marketplace_product.id,
                is_active=True,
            )
            db.add(product2)
            db.commit()

    def test_product_default_values(self, db, test_vendor, test_marketplace_product):
        """Test Product model default values."""
        product = Product(
            vendor_id=test_vendor.id,
            marketplace_product_id=test_marketplace_product.id,
        )
        db.add(product)
        db.commit()
        db.refresh(product)

        assert product.is_active is True  # Default
        assert product.is_featured is False  # Default
        assert product.min_quantity == 1  # Default
        assert product.display_order == 0  # Default

    def test_product_vendor_override_fields(self, db, test_vendor, test_marketplace_product):
        """Test Product model vendor-specific override fields."""
        product = Product(
            vendor_id=test_vendor.id,
            marketplace_product_id=test_marketplace_product.id,
            product_id="CUSTOM_SKU_001",
            price=49.99,
            sale_price=39.99,
            currency="USD",
            availability="limited",
            condition="new",
        )
        db.add(product)
        db.commit()
        db.refresh(product)

        assert product.product_id == "CUSTOM_SKU_001"
        assert product.price == 49.99
        assert product.sale_price == 39.99
        assert product.currency == "USD"
        assert product.availability == "limited"

    def test_product_inventory_settings(self, db, test_vendor, test_marketplace_product):
        """Test Product model inventory settings."""
        product = Product(
            vendor_id=test_vendor.id,
            marketplace_product_id=test_marketplace_product.id,
            min_quantity=2,
            max_quantity=10,
        )
        db.add(product)
        db.commit()
        db.refresh(product)

        assert product.min_quantity == 2
        assert product.max_quantity == 10

    def test_product_relationships(self, db, test_vendor, test_marketplace_product):
        """Test Product relationships."""
        product = Product(
            vendor_id=test_vendor.id,
            marketplace_product_id=test_marketplace_product.id,
        )
        db.add(product)
        db.commit()
        db.refresh(product)

        assert product.vendor is not None
        assert product.marketplace_product is not None
        assert product.inventory_entries == []  # No inventory yet
