from __future__ import annotations

from datetime import datetime
from typing import Literal

from pydantic import BaseModel, Field

from app.schemas.reimbursement import ExpenseClaimRead


class ApprovalWorkbenchPriorityReasonRead(BaseModel):
    code: str
    label: str
    weight: int = 0
    tone: Literal["normal", "warning", "danger"] = "normal"


class ApprovalWorkbenchEvidenceRead(BaseModel):
    completeness: float = 0.0
    present_count: int = 0
    required_count: int = 0
    missing_labels: list[str] = Field(default_factory=list)
    historical_labels: list[str] = Field(default_factory=list)


class ApprovalWorkbenchSuggestionRead(BaseModel):
    action: Literal[
        "manual_review",
        "request_supplement",
        "budget_review",
        "approve_candidate",
    ]
    label: str
    reason: str
    advisory_only: Literal[True] = True


class ApprovalWorkbenchItemRead(BaseModel):
    task_key: str
    claim: ExpenseClaimRead
    priority_score: int = 0
    priority_tier: Literal["normal", "high", "urgent"] = "normal"
    priority_reasons: list[ApprovalWorkbenchPriorityReasonRead] = Field(default_factory=list)
    risk_level: Literal["low", "medium", "high", "critical"] = "low"
    open_risk_count: int = 0
    budget_usage_rate: float | None = None
    waiting_hours: float = 0.0
    sla_due_at: datetime | None = None
    sla_overdue: bool = False
    evidence: ApprovalWorkbenchEvidenceRead
    suggestion: ApprovalWorkbenchSuggestionRead


class ApprovalWorkbenchListRead(BaseModel):
    items: list[ApprovalWorkbenchItemRead] = Field(default_factory=list)
    total: int = 0
    generated_at: datetime
    scoring_version: str = "approval_workbench_priority.v1"
