# Copyright 2026 ResQ
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""PDIE domain models for the ResQ MCP server."""

from __future__ import annotations

from datetime import datetime
from typing import Literal

from pydantic import BaseModel, Field

from resq_mcp.core.models import _utc_now


class VulnerabilityMap(BaseModel):
    """Static vulnerability assessment data for a geographic sector.

    Part of PDIE (Predictive Disaster Intelligence Engine) system.
    Contains precomputed risk factors, infrastructure data, and population
    metrics used for predictive disaster modeling and resource allocation.

    Attributes:
        sector_id: Sector identifier this map applies to.
        population_density: Human population density category.
        critical_infrastructure: List of critical facilities (e.g., "hospital", "power-substation").
        flood_risk: Flood vulnerability score (0.0 to 1.0).
        fire_risk: Fire vulnerability score (0.0 to 1.0).
        last_updated: UTC timestamp of last data update (auto-generated).

    Note:
        Risk scores are precomputed from historical data, terrain analysis,
        and infrastructure density. Updated periodically via GIS integration.
    """

    sector_id: str
    population_density: Literal["low", "medium", "high"]
    critical_infrastructure: list[str]
    flood_risk: float
    fire_risk: float
    last_updated: datetime = Field(default_factory=_utc_now)


class PreAlert(BaseModel):
    """Probabilistic disaster forecast from LSTM/GNN predictive models.

    Part of PDIE system. Generated by machine learning models that analyze
    weather patterns, sensor data, and historical trends to predict potential
    disasters before they occur. Enables proactive resource positioning.

    Attributes:
        alert_id: Unique alert identifier (e.g., "PRE-A1B2C3D4").
        sector_id: Target sector for the prediction.
        predicted_disaster_type: Expected disaster type (e.g., "wildfire", "flood").
        probability: Forecast confidence (0.0 to 1.0).
        forecast_horizon_hours: Time until predicted event (hours from now).
        vulnerability_context: Associated sector vulnerability data.
        generated_at: UTC timestamp of forecast generation (auto-generated).

    Example:
        >>> alert = PreAlert(
        ...     alert_id="PRE-123ABC",
        ...     sector_id="Sector-1",
        ...     predicted_disaster_type="wildfire",
        ...     probability=0.85,
        ...     forecast_horizon_hours=12,
        ...     vulnerability_context=vuln_map
        ... )
    """

    alert_id: str
    sector_id: str
    predicted_disaster_type: str
    probability: float
    forecast_horizon_hours: int
    vulnerability_context: VulnerabilityMap
    generated_at: datetime = Field(default_factory=_utc_now)
