# tests/unit/services/test_vendor_product_service.py
"""
Unit tests for VendorProductService.

Tests the vendor product catalog service operations.
"""

import pytest

from app.exceptions import ProductNotFoundException
from app.services.vendor_product_service import VendorProductService


@pytest.mark.unit
@pytest.mark.products
class TestVendorProductService:
    """Tests for VendorProductService."""

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

    def test_get_products_success(self, db, test_product):
        """Test getting vendor 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["vendor_id"] == test_product.vendor_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_vendor_filter(self, db, test_product, test_vendor):
        """Test getting products filtered by vendor."""
        products, total = self.service.get_products(db, vendor_id=test_vendor.id)

        assert total >= 1
        # All products should be from the filtered vendor
        for p in products:
            assert p["vendor_id"] == test_vendor.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 vendor 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 vendor 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_vendor" in stats
        assert stats["total"] >= 1

    def test_get_catalog_vendors_success(self, db, test_product, test_vendor):
        """Test getting list of vendors with products."""
        vendors = self.service.get_catalog_vendors(db)

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

        # Check that test_vendor is in the list
        vendor_ids = [v["id"] for v in vendors]
        assert test_vendor.id in vendor_ids

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

        assert product["id"] == test_product.id
        assert product["vendor_id"] == test_product.vendor_id
        assert product["marketplace_product_id"] == test_product.marketplace_product_id
        assert "source_marketplace" in product
        assert "source_vendor" 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 vendor 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)
