"""
Risk Pydantic schemas — extracted from compliance/api/schemas.py.

Phase 1 Step 3: the monolithic ``compliance.api.schemas`` module is being
split per domain under ``compliance.schemas``. This module is re-exported
from ``compliance.api.schemas`` for backwards compatibility.
"""

from datetime import datetime, date
from typing import Optional, List, Any, Dict

from pydantic import BaseModel, ConfigDict, Field

from compliance.schemas.common import (
    PaginationMeta, RegulationType, ControlType, ControlDomain,
    ControlStatus, RiskLevel, EvidenceStatus,
)


# ============================================================================
# Risk Schemas
# ============================================================================

class RiskBase(BaseModel):
    risk_id: str
    title: str
    description: Optional[str] = None
    category: str
    likelihood: int = Field(ge=1, le=5)
    impact: int = Field(ge=1, le=5)
    mitigating_controls: Optional[List[str]] = None
    owner: Optional[str] = None
    treatment_plan: Optional[str] = None


class RiskCreate(RiskBase):
    pass


class RiskUpdate(BaseModel):
    title: Optional[str] = None
    description: Optional[str] = None
    category: Optional[str] = None
    likelihood: Optional[int] = Field(default=None, ge=1, le=5)
    impact: Optional[int] = Field(default=None, ge=1, le=5)
    residual_likelihood: Optional[int] = Field(default=None, ge=1, le=5)
    residual_impact: Optional[int] = Field(default=None, ge=1, le=5)
    mitigating_controls: Optional[List[str]] = None
    owner: Optional[str] = None
    status: Optional[str] = None
    treatment_plan: Optional[str] = None


class RiskResponse(RiskBase):
    id: str
    inherent_risk: str
    residual_likelihood: Optional[int] = None
    residual_impact: Optional[int] = None
    residual_risk: Optional[str] = None
    status: str
    identified_date: Optional[date] = None
    review_date: Optional[date] = None
    last_assessed_at: Optional[datetime] = None
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


class RiskListResponse(BaseModel):
    risks: List[RiskResponse]
    total: int


class RiskMatrixResponse(BaseModel):
    """Risk matrix data for visualization."""
    matrix: Dict[str, Dict[str, List[str]]]  # likelihood -> impact -> risk_ids
    risks: List[RiskResponse]

