# tests/integration/api/v1/admin/test_vendors.py
"""Integration tests for admin vendor management endpoints.

Tests the /api/v1/admin/vendors/* endpoints.
"""

import pytest


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.admin
class TestAdminVendorsAPI:
    """Test admin vendor management endpoints at /api/v1/admin/vendors/*."""

    def test_get_all_vendors_admin(self, client, admin_headers, test_vendor):
        """Test admin getting all vendors."""
        response = client.get("/api/v1/admin/vendors", headers=admin_headers)

        assert response.status_code == 200
        data = response.json()
        assert data["total"] >= 1
        assert len(data["vendors"]) >= 1

        # Check that test_vendor is in the response
        vendor_codes = [
            vendor["vendor_code"]
            for vendor in data["vendors"]
            if "vendor_code" in vendor
        ]
        assert test_vendor.vendor_code in vendor_codes

    def test_get_all_vendors_non_admin(self, client, auth_headers):
        """Test non-admin trying to access admin vendor endpoint."""
        response = client.get("/api/v1/admin/vendors", headers=auth_headers)

        assert response.status_code == 403
        data = response.json()
        assert data["error_code"] == "ADMIN_REQUIRED"

    def test_toggle_vendor_verification_admin(self, client, admin_headers, test_vendor):
        """Test admin setting vendor verification status."""
        response = client.put(
            f"/api/v1/admin/vendors/{test_vendor.id}/verification",
            headers=admin_headers,
            json={"is_verified": True},
        )

        assert response.status_code == 200
        data = response.json()
        assert "id" in data
        assert "vendor_code" in data
        assert "is_verified" in data

    def test_toggle_vendor_verification_not_found(self, client, admin_headers):
        """Test admin verifying non-existent vendor."""
        response = client.put(
            "/api/v1/admin/vendors/99999/verification",
            headers=admin_headers,
            json={"is_verified": True},
        )

        assert response.status_code == 404
        data = response.json()
        assert data["error_code"] == "VENDOR_NOT_FOUND"
        assert "99999" in data["message"]
        assert "not found" in data["message"]

    def test_toggle_vendor_status_admin(self, client, admin_headers, test_vendor):
        """Test admin setting vendor active status."""
        response = client.put(
            f"/api/v1/admin/vendors/{test_vendor.id}/status",
            headers=admin_headers,
            json={"is_active": False},
        )

        assert response.status_code == 200
        data = response.json()
        assert "id" in data
        assert "vendor_code" in data
        assert "is_active" in data

    def test_toggle_vendor_status_not_found(self, client, admin_headers):
        """Test admin toggling status for non-existent vendor."""
        response = client.put(
            "/api/v1/admin/vendors/99999/status",
            headers=admin_headers,
            json={"is_active": True},
        )

        assert response.status_code == 404
        data = response.json()
        assert data["error_code"] == "VENDOR_NOT_FOUND"

    def test_get_vendor_statistics(self, client, admin_headers):
        """Test admin getting vendor statistics."""
        response = client.get("/api/v1/admin/vendors/stats", headers=admin_headers)

        assert response.status_code == 200
        data = response.json()
        assert "total" in data
        assert "verified" in data
        assert "pending" in data
        assert "inactive" in data
        assert isinstance(data["total"], int)

    def test_admin_pagination_vendors(self, client, admin_headers, test_vendor):
        """Test vendor pagination works correctly."""
        response = client.get(
            "/api/v1/admin/vendors?skip=0&limit=1", headers=admin_headers
        )
        assert response.status_code == 200
        data = response.json()
        assert data["total"] >= 1
        assert len(data["vendors"]) >= 0
        assert "skip" in data
        assert "limit" in data
