from datetime import datetime
from typing import List, Optional

from sqlmodel import Column, DateTime, Field, Relationship, SQLModel


class ObserverBase(SQLModel):
    phone: str
    email: str

    class Config:
        anystr_strip_whitespace = True
        anystr_lower = True


class Observer(ObserverBase, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    measurements: List["Measurement"] = Relationship(
        back_populates="observer", sa_relationship_kwargs={"cascade": "all,delete"}
    )


class ObserverCreate(ObserverBase):
    pass


class ObserverRead(ObserverBase):
    id: int


class MeasurementBase(SQLModel):
    temperaturescale: str
    temperature: int
    organizationid: int
    siteid: int
    date_time: Optional[datetime] = Field(
        sa_column=Column(DateTime, default=datetime.utcnow)
    )
    observer_id: Optional[int] = Field(default=None, foreign_key="observer.id")

    class Config:
        anystr_strip_whitespace = True
        anystr_lower = True


class Measurement(MeasurementBase, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    observer: Optional[Observer] = Relationship(back_populates="measurements")


class MeasurementCreate(MeasurementBase):
    pass


class MeasurementRead(MeasurementBase):
    id: int

sqlmodel.Field sqlmodel.Column sqlmodel.Relationship
[((350, 387), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (355, 387), False, 'from sqlmodel import Column, DateTime, Field, Relationship, SQLModel\n'), ((428, 521), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""observer"""', 'sa_relationship_kwargs': "{'cascade': 'all,delete'}"}), "(back_populates='observer', sa_relationship_kwargs={'cascade':\n    'all,delete'})\n", (440, 521), False, 'from sqlmodel import Column, DateTime, Field, Relationship, SQLModel\n'), ((891, 937), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""observer.id"""'}), "(default=None, foreign_key='observer.id')\n", (896, 937), False, 'from sqlmodel import Column, DateTime, Field, Relationship, SQLModel\n'), ((1098, 1135), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (1103, 1135), False, 'from sqlmodel import Column, DateTime, Field, Relationship, SQLModel\n'), ((1171, 1214), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""measurements"""'}), "(back_populates='measurements')\n", (1183, 1214), False, 'from sqlmodel import Column, DateTime, Field, Relationship, SQLModel\n'), ((810, 851), 'sqlmodel.Column', 'Column', (['DateTime'], {'default': 'datetime.utcnow'}), '(DateTime, default=datetime.utcnow)\n', (816, 851), False, 'from sqlmodel import Column, DateTime, Field, Relationship, SQLModel\n')]