from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime

class EventBase(BaseModel):
    event_type: str = Field(..., description="事件类型")
    device_id: int = Field(..., description="关联设备ID")
    algorithm_id: Optional[int] = Field(None, description="关联算法ID")
    severity: str = Field("medium", description="严重程度")
    status: str = Field("pending", description="事件状态")
    confidence: Optional[float] = Field(None, description="置信度")
    bbox: Optional[str] = Field(None, description="边界框坐标")
    image_path: Optional[str] = Field(None, description="事件图片路径")
    video_path: Optional[str] = Field(None, description="事件视频路径")
    description: Optional[str] = Field(None, description="事件描述")
    location: Optional[str] = Field(None, description="事件发生位置")
    detected_objects: Optional[str] = Field(None, description="检测到的对象")
    processing_time: Optional[float] = Field(None, description="处理时间")
    is_alert: bool = Field(False, description="是否触发告警")
    alert_sent: bool = Field(False, description="是否已发送告警")
    operator_id: Optional[int] = Field(None, description="处理人员ID")
    resolution_notes: Optional[str] = Field(None, description="处理备注")

class EventCreate(EventBase):
    pass

class EventUpdate(BaseModel):
    event_type: Optional[str] = None
    device_id: Optional[int] = None
    algorithm_id: Optional[int] = None
    severity: Optional[str] = None
    status: Optional[str] = None
    confidence: Optional[float] = None
    bbox: Optional[str] = None
    image_path: Optional[str] = None
    video_path: Optional[str] = None
    description: Optional[str] = None
    location: Optional[str] = None
    detected_objects: Optional[str] = None
    processing_time: Optional[float] = None
    is_alert: Optional[bool] = None
    alert_sent: Optional[bool] = None
    operator_id: Optional[int] = None
    resolution_notes: Optional[str] = None

class EventResponse(EventBase):
    id: int
    created_at: datetime
    updated_at: datetime
    resolved_at: Optional[datetime] = None

    class Config:
        from_attributes = True

class EventListResponse(BaseModel):
    events: List[EventResponse]
    total: int
    page: int
    size: int 