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


class EqpChecklistItemCreate(BaseModel):
    item_name: str
    item_standard: Optional[str] = None
    sort_order: int = 0


class EqpChecklistItemOut(EqpChecklistItemCreate):
    id: int
    checklist_id: int
    created_at: datetime
    class Config:
        from_attributes = True


class EqpChecklistCreate(BaseModel):
    name: str
    description: Optional[str] = None
    equipment_code: Optional[str] = None
    equipment_name: Optional[str] = None
    period: str = "daily"
    items: Optional[List[EqpChecklistItemCreate]] = None


class EqpChecklistUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    equipment_code: Optional[str] = None
    equipment_name: Optional[str] = None
    period: Optional[str] = None
    is_active: Optional[bool] = None


class EqpChecklistOut(BaseModel):
    id: int
    name: str
    description: Optional[str]
    equipment_code: Optional[str]
    equipment_name: Optional[str]
    period: str
    is_active: bool
    created_at: datetime
    class Config:
        from_attributes = True


class EqpInspectionDetailCreate(BaseModel):
    checklist_item_id: Optional[int] = None
    item_name: str
    actual_value: Optional[str] = None
    is_ok: bool = True
    notes: Optional[str] = None


class EqpInspectionDetailOut(EqpInspectionDetailCreate):
    id: int
    record_id: int
    created_at: datetime
    class Config:
        from_attributes = True


class EqpInspectionRecordCreate(BaseModel):
    checklist_id: int
    inspector: str
    inspect_time: datetime
    status: str = "ok"
    overall_result: Optional[str] = None
    remark: Optional[str] = None
    details: Optional[List[EqpInspectionDetailCreate]] = None


class EqpInspectionRecordOut(BaseModel):
    id: int
    checklist_id: int
    checklist_name: Optional[str]
    inspector: str
    inspect_time: datetime
    status: str
    overall_result: Optional[str]
    remark: Optional[str]
    created_at: datetime
    class Config:
        from_attributes = True
