# tests/test_integration.py
import pytest


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.e2e
class TestIntegrationFlows:
    def test_full_product_workflow(self, client, auth_headers):
        """Test complete product creation and management workflow"""
        # 1. Create a product
        product_data = {
            "marketplace_product_id": "FLOW001",
            "title": "Integration Test MarketplaceProduct",
            "description": "Testing full workflow",
            "price": "29.99",
            "brand": "FlowBrand",
            "gtin": "1111222233334",
            "availability": "in inventory",
            "marketplace": "TestFlow",
        }

        response = client.post(
            "/api/v1/marketplace/product", headers=auth_headers, json=product_data
        )
        assert response.status_code == 200
        product = response.json()

        # 2. Add inventory for the product
        inventory_data = {
            "gtin": product["gtin"],
            "location": "MAIN_WAREHOUSE",
            "quantity": 50,
        }

        response = client.post(
            "/api/v1/inventory", headers=auth_headers, json=inventory_data
        )
        assert response.status_code == 200

        # 3. Get product with inventory info
        response = client.get(
            f"/api/v1/marketplace/product/{product['marketplace_product_id']}",
            headers=auth_headers,
        )
        assert response.status_code == 200
        product_detail = response.json()
        assert product_detail["inventory_info"]["total_quantity"] == 50

        # 4. Update product
        update_data = {"title": "Updated Integration Test MarketplaceProduct"}
        response = client.put(
            f"/api/v1/marketplace/product/{product['marketplace_product_id']}",
            headers=auth_headers,
            json=update_data,
        )
        assert response.status_code == 200

        # 5. Search for product
        response = client.get(
            "/api/v1/marketplace/product?search=Updated Integration",
            headers=auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["total"] == 1

    def test_product_workflow(self, client, auth_headers):
        """Test vendor creation and product management workflow"""
        # 1. Create a vendor
        vendor_data = {
            "vendor_code": "FLOWVENDOR",
            "name": "Integration Flow Vendor",
            "description": "Test vendor for integration",
        }

        response = client.post("/api/v1/vendor", headers=auth_headers, json=vendor_data)
        assert response.status_code == 200
        vendor = response.json()

        # 2. Create a product
        product_data = {
            "marketplace_product_id": "VENDORFLOW001",
            "title": "Vendor Flow MarketplaceProduct",
            "price": "15.99",
            "marketplace": "VendorFlow",
        }

        response = client.post(
            "/api/v1/marketplace/product", headers=auth_headers, json=product_data
        )
        assert response.status_code == 200
        product = response.json()

        # 3. Add product to vendor (if endpoint exists)
        # This would test the vendor -product association

        # 4. Get vendor details
        response = client.get(
            f"/api/v1/vendor/{vendor['vendor_code']}", headers=auth_headers
        )
        assert response.status_code == 200

    def test_inventory_operations_workflow(self, client, auth_headers):
        """Test complete inventory management workflow"""
        gtin = "9999888877776"
        location = "TEST_WAREHOUSE"

        # 1. Set initial inventory
        response = client.post(
            "/api/v1/inventory",
            headers=auth_headers,
            json={"gtin": gtin, "location": location, "quantity": 100},
        )
        assert response.status_code == 200

        # 2. Add more inventory
        response = client.post(
            "/api/v1/inventory/add",
            headers=auth_headers,
            json={"gtin": gtin, "location": location, "quantity": 25},
        )
        assert response.status_code == 200
        assert response.json()["quantity"] == 125

        # 3. Remove some inventory
        response = client.post(
            "/api/v1/inventory/remove",
            headers=auth_headers,
            json={"gtin": gtin, "location": location, "quantity": 30},
        )
        assert response.status_code == 200
        assert response.json()["quantity"] == 95

        # 4. Check total inventory
        response = client.get(f"/api/v1/inventory/{gtin}/total", headers=auth_headers)
        assert response.status_code == 200
        assert response.json()["total_quantity"] == 95
