from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field


class NetworkOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    name: str
    description: str
    common_threats: str
    recommended_protection: str


class ThreatOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    name: str
    description: str
    probability: int
    confidentiality: int
    integrity: int
    availability: int
    affected_assets: str
    category: str


class TechnologyOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    name: str
    description: str
    effectiveness: float
    protection_type: str
    target_threats: str
    advantages: str
    limitations: str
    suitable_networks: str


class RecommendationOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    threat_id: int
    technology_id: int
    recommendation_text: str
    priority: str


class AnalysisRequest(BaseModel):
    network_type: str = Field(min_length=2, max_length=80)
    threat_id: int
    probability: int = Field(ge=1, le=5)
    confidentiality: int = Field(ge=1, le=5)
    integrity: int = Field(ge=1, le=5)
    availability: int = Field(ge=1, le=5)
    asset_value: int = Field(ge=1, le=5)
    selected_technologies: list[int] = Field(default_factory=list)


class AnalysisResultOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    network_type: str
    threat_name: str
    probability: int
    confidentiality: int
    integrity: int
    availability: int
    asset_value: int
    cia_score: float
    initial_risk: float
    risk_level: str
    protection_effectiveness: float
    residual_risk: float
    risk_reduction: float
    security_index: float
    security_status: str
    priority_score: float
    selected_technologies: str
    recommendations: str
    created_at: datetime


class AnalysisResponse(BaseModel):
    id: int
    network_type: str
    threat_name: str
    cia_score: float
    initial_risk: float
    risk_level: str
    protection_effectiveness: float
    residual_risk: float
    residual_risk_level: str
    risk_reduction: float
    security_index: float
    security_status: str
    priority_score: float
    selected_technologies: list[str]
    recommendations: list[str]
    created_at: datetime


class DashboardOut(BaseModel):
    total_threats: int
    critical_risks: int
    high_risks: int
    medium_risks: int
    low_risks: int
    average_security_index: float
    latest_results: list[AnalysisResultOut]
    risk_distribution: list[dict[str, int | str]]
    cia_impact: list[dict[str, float | str]]
    technology_effectiveness: list[dict[str, float | str]]
