# tests/unit/models/database/test_store.py
"""Unit tests for Store database model."""

import pytest
from sqlalchemy.exc import IntegrityError

from app.modules.tenancy.models import Store


@pytest.mark.unit
@pytest.mark.database
class TestStoreModel:
    """Test Store model."""

    def test_store_creation(self, db, test_merchant):
        """Test Store model creation with merchant relationship."""
        store = Store(
            merchant_id=test_merchant.id,
            store_code="DBTEST",
            subdomain="dbtest",
            name="Database Test Store",
            description="Testing store model",
            contact_email="contact@dbtest.com",
            contact_phone="+1234567890",
            business_address="123 Test Street",
            is_active=True,
            is_verified=False,
        )

        db.add(store)
        db.commit()
        db.refresh(store)

        assert store.id is not None
        assert store.store_code == "DBTEST"
        assert store.subdomain == "dbtest"
        assert store.name == "Database Test Store"
        assert store.merchant_id == test_merchant.id
        assert store.contact_email == "contact@dbtest.com"
        assert store.is_active is True
        assert store.is_verified is False
        assert store.created_at is not None

    def test_store_with_letzshop_urls(self, db, test_merchant):
        """Test Store model with multi-language Letzshop URLs."""
        store = Store(
            merchant_id=test_merchant.id,
            store_code="MULTILANG",
            subdomain="multilang",
            name="Multi-Language Store",
            letzshop_csv_url_fr="https://example.com/feed_fr.csv",
            letzshop_csv_url_en="https://example.com/feed_en.csv",
            letzshop_csv_url_de="https://example.com/feed_de.csv",
            is_active=True,
        )

        db.add(store)
        db.commit()
        db.refresh(store)

        assert store.letzshop_csv_url_fr == "https://example.com/feed_fr.csv"
        assert store.letzshop_csv_url_en == "https://example.com/feed_en.csv"
        assert store.letzshop_csv_url_de == "https://example.com/feed_de.csv"

    def test_store_code_uniqueness(self, db, test_merchant):
        """Test store_code unique constraint."""
        store1 = Store(
            merchant_id=test_merchant.id,
            store_code="UNIQUE",
            subdomain="unique1",
            name="Unique Store 1",
        )
        db.add(store1)
        db.commit()

        # Duplicate store_code should raise error
        with pytest.raises(IntegrityError):
            store2 = Store(
                merchant_id=test_merchant.id,
                store_code="UNIQUE",
                subdomain="unique2",
                name="Unique Store 2",
            )
            db.add(store2)
            db.commit()

    def test_subdomain_uniqueness(self, db, test_merchant):
        """Test subdomain unique constraint."""
        store1 = Store(
            merchant_id=test_merchant.id,
            store_code="STORE1",
            subdomain="testsubdomain",
            name="Store 1",
        )
        db.add(store1)
        db.commit()

        # Duplicate subdomain should raise error
        with pytest.raises(IntegrityError):
            store2 = Store(
                merchant_id=test_merchant.id,
                store_code="STORE2",
                subdomain="testsubdomain",
                name="Store 2",
            )
            db.add(store2)
            db.commit()

    def test_store_default_values(self, db, test_merchant):
        """Test Store model default values."""
        store = Store(
            merchant_id=test_merchant.id,
            store_code="DEFAULTS",
            subdomain="defaults",
            name="Default Store",
        )
        db.add(store)
        db.commit()
        db.refresh(store)

        assert store.is_active is True  # Default
        assert store.is_verified is False  # Default

    def test_store_merchant_relationship(self, db, test_merchant):
        """Test Store-Merchant relationship."""
        store = Store(
            merchant_id=test_merchant.id,
            store_code="RELTEST",
            subdomain="reltest",
            name="Relationship Test Store",
        )
        db.add(store)
        db.commit()
        db.refresh(store)

        assert store.merchant is not None
        assert store.merchant.id == test_merchant.id
        assert store.merchant.name == test_merchant.name
