# tests/unit/services/test_store_product_service.py
"""
Unit tests for StoreProductService.

Tests the store product catalog service operations.
"""

import pytest

from app.modules.catalog.exceptions import ProductNotFoundException
from app.modules.catalog.services.store_product_service import StoreProductService


@pytest.mark.unit
@pytest.mark.products
class TestStoreProductService:
    """Tests for StoreProductService."""

    def setup_method(self):
        self.service = StoreProductService()

    def test_get_products_success(self, db, test_product):
        """Test getting store products list."""
        products, total = self.service.get_products(db)

        assert total >= 1
        assert len(products) >= 1

        # Find our test product in results
        found = False
        for p in products:
            if p["id"] == test_product.id:
                found = True
                assert p["store_id"] == test_product.store_id
                assert (
                    p["marketplace_product_id"] == test_product.marketplace_product_id
                )
                break

        assert found, "Test product not found in results"

    def test_get_products_with_store_filter(self, db, test_product, test_store):
        """Test getting products filtered by store."""
        products, total = self.service.get_products(db, store_id=test_store.id)

        assert total >= 1
        # All products should be from the filtered store
        for p in products:
            assert p["store_id"] == test_store.id

    def test_get_products_with_active_filter(self, db, test_product):
        """Test getting products filtered by active status."""
        products, total = self.service.get_products(db, is_active=True)

        # All products should be active
        for p in products:
            assert p["is_active"] is True

    def test_get_products_with_featured_filter(self, db, test_product):
        """Test getting products filtered by featured status."""
        products, total = self.service.get_products(db, is_featured=False)

        # All products should not be featured
        for p in products:
            assert p["is_featured"] is False

    def test_get_products_pagination(self, db, test_product):
        """Test store products pagination."""
        products, total = self.service.get_products(db, skip=0, limit=10)

        assert total >= 1
        assert len(products) <= 10

    def test_get_product_stats_success(self, db, test_product):
        """Test getting store product statistics."""
        stats = self.service.get_product_stats(db)

        assert "total" in stats
        assert "active" in stats
        assert "inactive" in stats
        assert "featured" in stats
        assert "digital" in stats
        assert "physical" in stats
        assert "by_store" in stats
        assert stats["total"] >= 1

    def test_get_catalog_stores_success(self, db, test_product, test_store):
        """Test getting list of stores with products."""
        stores = self.service.get_catalog_stores(db)

        assert isinstance(stores, list)
        assert len(stores) >= 1

        # Check that test_store is in the list
        store_ids = [v["id"] for v in stores]
        assert test_store.id in store_ids

    def test_get_product_detail_success(self, db, test_product):
        """Test getting store product detail."""
        product = self.service.get_product_detail(db, test_product.id)

        assert product["id"] == test_product.id
        assert product["store_id"] == test_product.store_id
        assert product["marketplace_product_id"] == test_product.marketplace_product_id
        assert "source_marketplace" in product
        assert "source_store" in product

    def test_get_product_detail_not_found(self, db):
        """Test getting non-existent product raises exception."""
        with pytest.raises(ProductNotFoundException):
            self.service.get_product_detail(db, 99999)

    def test_remove_product_success(self, db, test_product):
        """Test removing product from store catalog."""
        product_id = test_product.id

        result = self.service.remove_product(db, product_id)

        assert "message" in result
        assert "removed" in result["message"].lower()

        # Verify product is removed
        with pytest.raises(ProductNotFoundException):
            self.service.get_product_detail(db, product_id)

    def test_remove_product_not_found(self, db):
        """Test removing non-existent product raises exception."""
        with pytest.raises(ProductNotFoundException):
            self.service.remove_product(db, 99999)
