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


class PlantingCreate(SQLModel):
    garden_id: int
    variety_id: int
    cell_id: Optional[int] = None
    date_semis: Optional[date] = None
    date_plantation: Optional[date] = None
    date_repiquage: Optional[date] = None
    quantite: int = 1
    statut: str = "prevu"
    date_recolte_debut: Optional[date] = None
    date_recolte_fin: Optional[date] = None
    rendement_estime: Optional[float] = None
    rendement_reel: Optional[float] = None
    notes: Optional[str] = None


class Planting(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    garden_id: int = Field(foreign_key="garden.id", index=True)
    variety_id: int = Field(foreign_key="plant.id", index=True)
    cell_id: Optional[int] = Field(default=None, foreign_key="gardencell.id")
    date_semis: Optional[date] = None
    date_plantation: Optional[date] = None
    date_repiquage: Optional[date] = None
    quantite: int = 1
    statut: str = "prevu"  # prevu | en_cours | termine | echoue
    date_recolte_debut: Optional[date] = None
    date_recolte_fin: Optional[date] = None
    rendement_estime: Optional[float] = None
    rendement_reel: Optional[float] = None
    notes: Optional[str] = None
    created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))


class PlantingEvent(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    planting_id: int = Field(foreign_key="planting.id", index=True)
    type: str  # arrosage | taille | traitement | observation | autre
    note: Optional[str] = None
    ts: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
