from datetime import datetime, timezone
from typing import Optional
from sqlmodel import Field, SQLModel


class Garden(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    nom: str
    description: Optional[str] = None
    type: str = "plein_air"  # plein_air | serre | tunnel
    latitude: Optional[float] = None
    longitude: Optional[float] = None
    altitude: Optional[float] = None
    adresse: Optional[str] = None
    exposition: Optional[str] = None
    ombre: Optional[str] = None  # ombre | mi-ombre | plein_soleil
    sol_type: Optional[str] = None
    sol_ph: Optional[float] = None
    surface_m2: Optional[float] = None
    ensoleillement: Optional[str] = None
    grille_largeur: int = 6
    grille_hauteur: int = 4
    created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))


class GardenCell(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    garden_id: int = Field(foreign_key="garden.id", index=True)
    col: int
    row: int
    libelle: Optional[str] = None
    largeur_m: Optional[float] = None
    hauteur_m: Optional[float] = None
    etat: str = "libre"  # libre | occupe
    notes: Optional[str] = None


class GardenImage(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    garden_id: int = Field(foreign_key="garden.id", index=True)
    filename: str
    caption: Optional[str] = None
    created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))


class Measurement(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    garden_id: int = Field(foreign_key="garden.id", index=True)
    temp_air: Optional[float] = None
    temp_sol: Optional[float] = None
    humidite_air: Optional[float] = None
    humidite_sol: Optional[float] = None
    source: str = "manuel"  # manuel | capteur
    ts: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
