from collections.abc import Generator
from uuid import UUID

import pytest
from fastapi.testclient import TestClient
from src.__types.index import ContractProvider
from src.contract_api import app
from src.routers.contract.authorizer import get_contract_provider
from src.routers.contract.contract_events_router import settings as router_settings


@pytest.fixture(scope="session")
def registered_identifiers() -> dict[str, str]:
    return {
        "contract_provider_whi_sfdc_id": "8f906216-8cfd-4df4-a6e9-1c5cc960ff74",
        "contract_provider_whi_sfdc_application_client_id": "672qqd6nnandh03b7f5c34k39p",
        "contract_provider_whi_sfdc_name": "whi_sfdc",
        "tenant_alpha_id": "00000001-0000-0000-0000-000000000000",
        "tenant_alpha_whi_customer_id": "whi#alpha_1",
    }


@pytest.fixture(scope="session")
def contract_provider_whi_sfdc(registered_identifiers: dict[str, str]) -> ContractProvider:
    return ContractProvider(
        id=UUID(registered_identifiers["contract_provider_whi_sfdc_id"]),
        application_client_id=registered_identifiers["contract_provider_whi_sfdc_application_client_id"],
        name=registered_identifiers["contract_provider_whi_sfdc_name"],
    )


@pytest.fixture
def mock_cognito_client(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(router_settings, "user_pool_id", "dummy_user_pool")
    monkeypatch.setattr(router_settings, "stage", "local")
    monkeypatch.setattr(router_settings, "aws_region", "ap-northeast-1")


@pytest.fixture
def test_client(contract_provider_whi_sfdc: ContractProvider) -> Generator[TestClient, None, None]:
    def get_authenticated_contract_provider() -> ContractProvider:
        return contract_provider_whi_sfdc

    app.dependency_overrides[get_contract_provider] = get_authenticated_contract_provider
    try:
        with TestClient(app) as test_client:
            yield test_client
    finally:
        app.dependency_overrides.pop(get_contract_provider, None)
