from datetime import datetime
from typing import Optional

from sqlmodel import Field, SQLModel


class AlarmRecordBase(SQLModel):
    # todo tree code?? alarm_category??
    device_code: str
    alarm_type: int
    alarm_content: str
    alarm_value: Optional[float] = None
    alarm_image: Optional[str] = None
    alarm_time: datetime = Field(default_factory=datetime.now)

    class Config:
        json_encoders = {
            datetime: lambda v: v.strftime('%Y-%m-%d %H:%M:%S')
        }


class AlarmRecord(AlarmRecordBase, table=True):
    __tablename__ = 'alarm_record'
    id: Optional[int] = Field(default=None, primary_key=True)


class AlarmRecordCreate(AlarmRecordBase):
    pass


class AlarmRecordInfo(AlarmRecordBase):
    id: int
