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


# ------------------------------
# 请求模型（新增记录用，极简）
# ------------------------------
class DeviceActionCreate(BaseModel):
    """设备操作记录创建模型（0=离线，1=上线）"""
    client_ip: str = Field(..., description="客户端IP")
    action: int = Field(..., ge=0, le=1, description="操作状态（0=离线，1=上线）")


# ------------------------------
# 响应模型（单条记录）
# ------------------------------
class DeviceActionResponse(BaseModel):
    """设备操作记录响应模型（与自增表对齐）"""
    id: int = Field(..., description="自增主键ID")
    client_ip: Optional[str] = Field(None, description="客户端IP")
    action: Optional[int] = Field(None, description="操作状态（0=离线，1=上线）")
    created_at: datetime = Field(..., description="记录创建时间")
    updated_at: datetime = Field(..., description="记录更新时间")

    # 支持从数据库结果直接转换
    model_config = {"from_attributes": True}


# ------------------------------
# 列表响应模型（仅含 total + device_actions）
# ------------------------------
class DeviceActionListResponse(BaseModel):
    """设备操作记录列表（仅核心返回字段）"""
    total: int = Field(..., description="总记录数")
    device_actions: List[DeviceActionResponse] = Field(..., description="操作记录列表")