# tests/integration/api/v1/admin/test_stores.py
"""Integration tests for admin store management endpoints.

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

import pytest


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.admin
class TestAdminStoresAPI:
    """Test admin store management endpoints at /api/v1/admin/stores/*."""

    def test_get_all_stores_admin(self, client, admin_headers, test_store):
        """Test admin getting all stores."""
        response = client.get("/api/v1/admin/stores", headers=admin_headers)

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

        # Check that test_store is in the response
        store_codes = [
            store["store_code"]
            for store in data["stores"]
            if "store_code" in store
        ]
        assert test_store.store_code in store_codes

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

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

    def test_toggle_store_verification_admin(self, client, admin_headers, test_store):
        """Test admin setting store verification status."""
        response = client.put(
            f"/api/v1/admin/stores/{test_store.id}/verification",
            headers=admin_headers,
            json={"is_verified": True},
        )

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

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

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

    def test_toggle_store_status_admin(self, client, admin_headers, test_store):
        """Test admin setting store active status."""
        response = client.put(
            f"/api/v1/admin/stores/{test_store.id}/status",
            headers=admin_headers,
            json={"is_active": False},
        )

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

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

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

    def test_get_store_statistics(self, client, admin_headers):
        """Test admin getting store statistics."""
        response = client.get("/api/v1/admin/stores/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_stores(self, client, admin_headers, test_store):
        """Test store pagination works correctly."""
        response = client.get(
            "/api/v1/admin/stores?skip=0&limit=1", headers=admin_headers
        )
        assert response.status_code == 200
        data = response.json()
        assert data["total"] >= 1
        assert len(data["stores"]) >= 0
        assert "skip" in data
        assert "limit" in data


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.admin
class TestAdminStoreCreationAPI:
    """Test admin store creation endpoints with platform assignment."""

    def test_create_store_without_platforms(
        self, client, admin_headers, test_merchant
    ):
        """Test creating a store without platform assignments."""
        import uuid

        unique_id = str(uuid.uuid4())[:8]
        response = client.post(
            "/api/v1/admin/stores",
            headers=admin_headers,
            json={
                "merchant_id": test_merchant.id,
                "store_code": f"NOPLAT_{unique_id}",
                "subdomain": f"noplat{unique_id}",
                "name": f"No Platform Store {unique_id}",
            },
        )

        assert response.status_code == 200
        data = response.json()
        assert data["store_code"] == f"NOPLAT_{unique_id}".upper()
        assert data["merchant_id"] == test_merchant.id

    def test_create_store_with_platforms(
        self, client, admin_headers, test_merchant, test_platform, another_platform
    ):
        """Test creating a store with platform assignments."""
        import uuid

        unique_id = str(uuid.uuid4())[:8]
        response = client.post(
            "/api/v1/admin/stores",
            headers=admin_headers,
            json={
                "merchant_id": test_merchant.id,
                "store_code": f"WITHPLAT_{unique_id}",
                "subdomain": f"withplat{unique_id}",
                "name": f"With Platform Store {unique_id}",
                "platform_ids": [test_platform.id, another_platform.id],
            },
        )

        assert response.status_code == 200
        data = response.json()
        assert data["store_code"] == f"WITHPLAT_{unique_id}".upper()

    def test_create_store_duplicate_code_fails(
        self, client, admin_headers, test_merchant, test_store
    ):
        """Test creating a store with duplicate code fails."""
        response = client.post(
            "/api/v1/admin/stores",
            headers=admin_headers,
            json={
                "merchant_id": test_merchant.id,
                "store_code": test_store.store_code,
                "subdomain": "uniquesubdomain123",
                "name": "Duplicate Code Store",
            },
        )

        assert response.status_code == 409  # Conflict
        data = response.json()
        assert data["error_code"] == "STORE_ALREADY_EXISTS"

    def test_create_store_non_admin_fails(
        self, client, auth_headers, test_merchant
    ):
        """Test non-admin cannot create stores."""
        import uuid

        unique_id = str(uuid.uuid4())[:8]
        response = client.post(
            "/api/v1/admin/stores",
            headers=auth_headers,
            json={
                "merchant_id": test_merchant.id,
                "store_code": f"NONADMIN_{unique_id}",
                "subdomain": f"nonadmin{unique_id}",
                "name": "Non Admin Store",
            },
        )

        assert response.status_code == 403
