import uuid
from types import ModuleType

import pytest
from fastapi import status
from fastapi.testclient import TestClient
from src.routers.client import usage_events_router


@pytest.fixture
def target_router_module() -> ModuleType:
    return usage_events_router


@pytest.mark.usefixtures("mock_s3_client", "mock_sqs_client")
class TestUsageEventsPost:
    PATH = "/v1/usage-events"

    def test_accepts_usage_events(
        self,
        test_client: TestClient,
        registered_identifiers: dict[str, str],
        header_names: dict[str, str],
    ) -> None:
        headers = {
            header_names["api_key"]: registered_identifiers["api_key_gen_ai_profile"],
            header_names["tenant_name"]: registered_identifiers["tenant_alpha_1"],
            header_names["idempotency_key"]: uuid.uuid4().hex,
        }
        payload = {
            "events": [
                {
                    "code": registered_identifiers["code_input_token"],
                    "quantity": "10",
                    "service": registered_identifiers["service_gen_ai_profile"],
                    "feature": registered_identifiers["feature_chat"],
                    "userId": "user-1",
                    "traceId": "trace-1",
                },
            ],
        }

        response = test_client.post(url=self.PATH, headers=headers, json=payload)
        assert response.status_code == status.HTTP_202_ACCEPTED
        assert response.content == b""

    def test_idempotency_key_missing(
        self,
        test_client: TestClient,
        registered_identifiers: dict[str, str],
        header_names: dict[str, str],
    ) -> None:
        headers = {
            header_names["api_key"]: registered_identifiers["api_key_gen_ai_profile"],
            header_names["tenant_name"]: registered_identifiers["tenant_alpha_1"],
        }
        payload = {
            "events": [
                {
                    "code": registered_identifiers["code_input_token"],
                    "quantity": "1",
                    "service": registered_identifiers["service_gen_ai_profile"],
                    "feature": registered_identifiers["feature_chat"],
                },
            ],
        }
        response = test_client.post(url=self.PATH, headers=headers, json=payload)
        assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT

    def test_request_body_invalid(
        self,
        test_client: TestClient,
        registered_identifiers: dict[str, str],
        header_names: dict[str, str],
    ) -> None:
        headers = {
            header_names["api_key"]: registered_identifiers["api_key_gen_ai_profile"],
            header_names["tenant_name"]: registered_identifiers["tenant_alpha_1"],
            header_names["idempotency_key"]: uuid.uuid4().hex,
        }
        payload = {"events": [{"quantity": "1"}]}
        response = test_client.post(url=self.PATH, headers=headers, json=payload)
        assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT

    def test_code_not_found(
        self,
        test_client: TestClient,
        registered_identifiers: dict[str, str],
        header_names: dict[str, str],
    ) -> None:
        headers = {
            header_names["api_key"]: registered_identifiers["api_key_gen_ai_profile"],
            header_names["tenant_name"]: registered_identifiers["tenant_alpha_1"],
            header_names["idempotency_key"]: uuid.uuid4().hex,
        }
        payload = {
            "events": [
                {
                    "code": "invalid.code",
                    "quantity": "1",
                    "service": registered_identifiers["service_gen_ai_profile"],
                    "feature": registered_identifiers["feature_chat"],
                },
            ],
        }
        response = test_client.post(url=self.PATH, headers=headers, json=payload)
        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert response.json()["detail"] == "Invalid code."
