from uuid import UUID

from fastapi import status
from fastapi.testclient import TestClient
from src.__types.index import Tenant
from src.management_api import app
from src.routers.management.authorizer import get_tenant, get_user_id


class TestUsersMeGet:
    PATH = "/v1/users/me"

    def test_returns_current_user(
        self,
        test_client_as_member: TestClient,
        registered_identifiers: dict[str, str],
    ) -> None:
        """認証済みユーザー自身の情報を取得できることを確認する。"""
        response = test_client_as_member.get(url=self.PATH)

        assert response.status_code == status.HTTP_200_OK
        assert response.json() == {
            "id": registered_identifiers["user_alpha_member_id"],
            "display_id": "0001",
            "display_name": "社員001#アルファ株式会社",
            "email": "test001@test.com",
        }

    def test_user_not_found(
        self,
        tenant_alpha: Tenant,
    ) -> None:
        """トークンのuser_idに対応するユーザーが存在しない場合に404を返すことを確認する。"""
        non_existent_user_id = UUID("99999999-9999-9999-9999-999999999999")

        def get_authenticated_tenant() -> Tenant:
            return tenant_alpha

        def get_authenticated_user_id() -> UUID:
            return non_existent_user_id

        app.dependency_overrides[get_tenant] = get_authenticated_tenant
        app.dependency_overrides[get_user_id] = get_authenticated_user_id
        try:
            with TestClient(app) as test_client:
                response = test_client.get(url=self.PATH)
        finally:
            app.dependency_overrides.pop(get_tenant, None)
            app.dependency_overrides.pop(get_user_id, None)

        assert response.status_code == status.HTTP_404_NOT_FOUND
        assert response.json() == {
            "detail": "Requested resource was not found.",
            "invalid_parameters": None,
        }
