from typing import Annotated, Literal

from fastapi import APIRouter, Depends, Query
from psycopg import Connection
from pydantic import BaseModel, ConfigDict, Field
from src.__types.index import Tenant
from src.domain.entities.quota_allocation import QuotaAllocation, QuotaAllocationStatus, QuotaAllocationType
from src.routers.management.authorizer import get_tenant
from src.routers.management.connection import get_connection
from src.usecases.quota_usecase import QuotaUseCase

router = APIRouter(
    prefix="/quota-allocations",
    tags=["クォータ割り当て"],
)


class QuotaAllocationResponse(QuotaAllocation):
    in_use: bool = Field(
        ...,
        description="現在使用中かどうか(=最も有効期限が短いもの)",
    )


class GetResponse(BaseModel):
    quota_allocations: list[QuotaAllocationResponse] = Field(..., description="クォータ割り当てのリスト")
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "quota_allocations": [
                    {
                        "in_use": True,
                        "type": QuotaAllocationType.BASE,
                        "status": QuotaAllocationStatus.ACTIVE,
                        "amount": 100000,
                        "used": 50000,
                        "valid_from_date": "2025-01-01",
                        "valid_to_date": "2025-12-31",
                    },
                    {
                        "in_use": False,
                        "type": QuotaAllocationType.TOP_UP,
                        "status": QuotaAllocationStatus.ACTIVE,
                        "amount": 100000,
                        "used": 0,
                        "valid_from_date": "2025-02-01",
                        "valid_to_date": "2026-01-31",
                    },
                ],
            },
        },
    )


AllowedQuotaAllocationStatus = Literal[
    QuotaAllocationStatus.ACTIVE,
    QuotaAllocationStatus.EXCEEDED,
    QuotaAllocationStatus.SCHEDULED,
]


@router.get(
    "",
    summary="取得",
    description="クォータ割り当てを取得します。",
)
def get(
    tenant: Annotated[Tenant, Depends(get_tenant)],
    status: Annotated[list[AllowedQuotaAllocationStatus], Query()],
    connection: Annotated[Connection, Depends(get_connection)],
) -> GetResponse:
    usecase = QuotaUseCase.create(tenant=tenant, connection=connection)
    in_use_allocation_id, allocations = usecase.get_allocations(
        status=[QuotaAllocationStatus(status) for status in status],
    )
    return GetResponse(
        quota_allocations=[
            QuotaAllocationResponse(
                id=allocation.id,
                type=allocation.type,
                status=allocation.status,
                amount=allocation.amount,
                used=allocation.used,
                valid_from_date=allocation.valid_from_date,
                valid_to_date=allocation.valid_to_date,
                # 現在使用中のクォータ割り当てかどうか
                in_use=allocation.id == in_use_allocation_id,
            )
            for allocation in allocations
        ],
    )
