from datetime import datetime
from typing import List, Optional
from uuid import UUID

from pydantic import Field

from src.models import BaseModel, Pagination
from src.aeros_equipment.schema import MasterEquipment, EquipmentWithCustomParameters

# Pydantic models for request/response validation
class SimulationInput(BaseModel):
    SchematicName: str = "- TJB - Unit 3 -"
    SimSeed: int = 1
    SimDuration: int = 3
    DurationUnit: str = "UYear"
    SimNumRun: int = 1
    SimulationName: str = "DefaultSimulation"
    CustomInput: dict = {}
    IsDefault:bool = False
    Konkin_offset: Optional[int] = 0

class SimulationNode(BaseModel):
    id: UUID
    node_type: Optional[str]
    node_id: Optional[int]
    node_name: Optional[str]
    structure_name: Optional[str]
    schematic_name: Optional[str]
    schematic_id: Optional[UUID]
    model_image: Optional[list] = Field(None)
    equipment:Optional[MasterEquipment] = None

class SimulationCalc(BaseModel):
    id: UUID
    total_downtime: float
    total_uptime: float
    num_events: float
    production: float
    production_std: float
    ideal_production: float
    availability: float
    efficiency: float
    effective_loss: float
    num_cm: float
    cm_waiting_time: float
    total_cm_downtime: float
    num_pm: float
    total_pm_downtime: float
    num_ip: float
    total_ip_downtime: float
    num_oh: float
    total_oh_downtime: float
    t_wait_for_crew: float
    t_wait_for_spare: float
    duration_at_full: float
    duration_above_hh: float
    duration_above_h: float
    duration_below_l: float
    duration_below_ll: float
    duration_at_empty: float
    stg_input: float
    eaf: float
    eaf_konkin: Optional[float]
    efor: Optional[float]
    derating_hours: Optional[float]
    aeros_node: SimulationNode
    contribution: Optional[float] = 0
    criticality: Optional[float]

class SimulationPlot(BaseModel):
    id: UUID
    max_flow_rate: float
    storage_capacity: float
    point_availabilities: list
    point_flowrates: list
    timestamp_outs: list
    aeros_node: SimulationNode



class SimulationNodeWithResult(SimulationNode):
    calc_results: List[SimulationCalc]


class SimulationCalcResult(BaseModel):
    id: UUID
    calc_results: List[SimulationCalc]


class SimulationPlotResult(BaseModel):
    id: UUID
    plot_results: List[SimulationPlot]

class SimulationData(BaseModel):
    id: UUID
    simulation_name: str 
    status: str
    schematic_name: str
    reliability: Optional[dict]
    created_at: datetime

class SimulationRankingParameters(EquipmentWithCustomParameters):
    availability:float


class SimulationPagination(Pagination):
    items: List[SimulationData] = []
