# tests/integration/api/v1/store/test_inventory.py
"""Integration tests for store inventory management endpoints.

Tests the /api/v1/store/inventory/* endpoints.
All endpoints require store JWT authentication.
"""

import pytest


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.store
class TestStoreInventoryAPI:
    """Test store inventory management endpoints at /api/v1/store/inventory/*."""

    def test_set_inventory_success(
        self,
        client,
        store_user_headers,
        test_store_with_store_user,
        test_product,
        db,
    ):
        """Test setting inventory for a product."""
        # Ensure test_product belongs to the store
        from app.modules.catalog.models import Product

        product = db.query(Product).filter(Product.id == test_product.id).first()
        product.store_id = test_store_with_store_user.id
        db.commit()

        inventory_data = {
            "product_id": test_product.id,
            "location": "WAREHOUSE_A",
            "quantity": 100,
        }

        response = client.post(
            "/api/v1/store/inventory/set",
            headers=store_user_headers,
            json=inventory_data,
        )

        assert response.status_code == 200, f"Failed: {response.json()}"
        data = response.json()
        assert data["product_id"] == test_product.id
        assert data["quantity"] == 100

    def test_adjust_inventory_success(
        self,
        client,
        store_user_headers,
        test_store_with_store_user,
        test_product,
        db,
    ):
        """Test adjusting inventory quantity."""
        from app.modules.catalog.models import Product

        product = db.query(Product).filter(Product.id == test_product.id).first()
        product.store_id = test_store_with_store_user.id
        db.commit()

        # First set initial inventory
        set_data = {
            "product_id": test_product.id,
            "location": "WAREHOUSE_A",
            "quantity": 100,
        }
        client.post(
            "/api/v1/store/inventory/set",
            headers=store_user_headers,
            json=set_data,
        )

        # Then adjust
        adjust_data = {
            "product_id": test_product.id,
            "location": "WAREHOUSE_A",
            "quantity": -10,
        }

        response = client.post(
            "/api/v1/store/inventory/adjust",
            headers=store_user_headers,
            json=adjust_data,
        )

        assert response.status_code == 200
        data = response.json()
        assert data["quantity"] == 90

    def test_get_store_inventory_success(self, client, store_user_headers):
        """Test getting store inventory list."""
        response = client.get(
            "/api/v1/store/inventory",
            headers=store_user_headers,
        )

        assert response.status_code == 200
        data = response.json()
        assert "inventories" in data or "items" in data or "inventory" in data
        assert "total" in data

    def test_get_store_inventory_with_pagination(self, client, store_user_headers):
        """Test getting store inventory with pagination."""
        response = client.get(
            "/api/v1/store/inventory?skip=0&limit=10",
            headers=store_user_headers,
        )

        assert response.status_code == 200
        data = response.json()
        assert "total" in data

    def test_get_product_inventory_success(
        self,
        client,
        store_user_headers,
        test_store_with_store_user,
        test_product,
        db,
    ):
        """Test getting inventory for a specific product."""
        from app.modules.catalog.models import Product

        product = db.query(Product).filter(Product.id == test_product.id).first()
        product.store_id = test_store_with_store_user.id
        db.commit()

        response = client.get(
            f"/api/v1/store/inventory/product/{test_product.id}",
            headers=store_user_headers,
        )

        assert response.status_code == 200
        data = response.json()
        assert "product_id" in data or "total_quantity" in data

    def test_get_inventory_without_auth_returns_unauthorized(self, client):
        """Test getting inventory without authentication returns unauthorized."""
        response = client.get("/api/v1/store/inventory")

        assert response.status_code == 401
        data = response.json()
        assert data["error_code"] == "INVALID_TOKEN"

    def test_reserve_inventory_success(
        self,
        client,
        store_user_headers,
        test_store_with_store_user,
        test_product,
        db,
    ):
        """Test reserving inventory for an order."""
        from app.modules.catalog.models import Product

        product = db.query(Product).filter(Product.id == test_product.id).first()
        product.store_id = test_store_with_store_user.id
        db.commit()

        # First set initial inventory
        set_data = {
            "product_id": test_product.id,
            "location": "WAREHOUSE_A",
            "quantity": 100,
        }
        client.post(
            "/api/v1/store/inventory/set",
            headers=store_user_headers,
            json=set_data,
        )

        # Then reserve
        reserve_data = {
            "product_id": test_product.id,
            "location": "WAREHOUSE_A",
            "quantity": 5,
            "order_id": 12345,
        }

        response = client.post(
            "/api/v1/store/inventory/reserve",
            headers=store_user_headers,
            json=reserve_data,
        )

        assert response.status_code == 200

    def test_update_inventory_success(
        self,
        client,
        store_user_headers,
        test_store_with_store_user,
        test_product,
        db,
    ):
        """Test updating inventory record."""
        from app.modules.catalog.models import Product

        product = db.query(Product).filter(Product.id == test_product.id).first()
        product.store_id = test_store_with_store_user.id
        db.commit()

        # First set initial inventory
        set_data = {
            "product_id": test_product.id,
            "location": "WAREHOUSE_A",
            "quantity": 100,
        }
        set_response = client.post(
            "/api/v1/store/inventory/set",
            headers=store_user_headers,
            json=set_data,
        )

        if set_response.status_code == 200:
            inventory_id = set_response.json().get("id")
            if inventory_id:
                update_data = {
                    "quantity": 150,
                }

                response = client.put(
                    f"/api/v1/store/inventory/{inventory_id}",
                    headers=store_user_headers,
                    json=update_data,
                )

                assert response.status_code == 200

    def test_delete_inventory_success(
        self,
        client,
        store_user_headers,
        test_store_with_store_user,
        test_product,
        db,
    ):
        """Test deleting inventory record."""
        from app.modules.catalog.models import Product

        product = db.query(Product).filter(Product.id == test_product.id).first()
        product.store_id = test_store_with_store_user.id
        db.commit()

        # First set initial inventory
        set_data = {
            "product_id": test_product.id,
            "location": "WAREHOUSE_DELETE",
            "quantity": 50,
        }
        set_response = client.post(
            "/api/v1/store/inventory/set",
            headers=store_user_headers,
            json=set_data,
        )

        if set_response.status_code == 200:
            inventory_id = set_response.json().get("id")
            if inventory_id:
                response = client.delete(
                    f"/api/v1/store/inventory/{inventory_id}",
                    headers=store_user_headers,
                )

                assert response.status_code == 200
