from datetime import datetime
from enum import Enum
from typing import List, Optional

from pydantic import BaseModel, Field


class AlertType(str, Enum):
    GENERAL = "general"
    MEDICAL = "medical"
    VIOLENCE = "violence"
    HARASSMENT = "harassment"
    UNSAFE_AREA = "unsafe_area"


class ResponseType(str, Enum):
    HELP_ON_WAY = "help_on_way"
    CONTACTED_AUTHORITIES = "contacted_authorities"
    SAFE_NOW = "safe_now"
    FALSE_ALARM = "false_alarm"


class EmergencyAlertCreate(BaseModel):
    latitude: float = Field(..., ge=-90, le=90)
    longitude: float = Field(..., ge=-180, le=180)
    alert_type: AlertType = AlertType.GENERAL
    message: Optional[str] = Field(None, max_length=500)
    address: Optional[str] = Field(None, max_length=500)


class EmergencyAlertResponse(BaseModel):
    id: int
    uuid: str
    user_id: int
    latitude: float
    longitude: float
    address: Optional[str]
    alert_type: str
    message: Optional[str]
    is_resolved: bool
    resolved_at: Optional[datetime]
    notified_users_count: int
    responded_users_count: int
    created_at: datetime

    class Config:
        from_attributes = True


class EmergencyResponseCreate(BaseModel):
    response_type: ResponseType
    message: Optional[str] = Field(None, max_length=500)
    eta_minutes: Optional[int] = Field(None, ge=0, le=240)  # Max 4 hours


class EmergencyResponseResponse(BaseModel):
    id: int
    alert_id: int
    responder_id: int
    response_type: str
    message: Optional[str]
    eta_minutes: Optional[int]
    created_at: datetime

    class Config:
        from_attributes = True


class NearbyUsersResponse(BaseModel):
    user_id: int
    distance_meters: float
    latitude: float
    longitude: float


class EmergencyStats(BaseModel):
    total_alerts: int
    active_alerts: int
    resolved_alerts: int
    avg_response_time_minutes: Optional[float]
    total_responders: int
