from datetime import datetime, timezone
from typing import List

from fastapi import APIRouter, Depends, HTTPException, status
from sqlmodel import Session, select

from app.database import get_session
from app.models.garden import Garden, GardenCell, GardenImage, Measurement

router = APIRouter(tags=["jardins"])


@router.get("/gardens", response_model=List[Garden])
def list_gardens(session: Session = Depends(get_session)):
    return session.exec(select(Garden)).all()


@router.post("/gardens", response_model=Garden, status_code=status.HTTP_201_CREATED)
def create_garden(garden: Garden, session: Session = Depends(get_session)):
    session.add(garden)
    session.commit()
    session.refresh(garden)
    return garden


@router.get("/gardens/{id}", response_model=Garden)
def get_garden(id: int, session: Session = Depends(get_session)):
    g = session.get(Garden, id)
    if not g:
        raise HTTPException(status_code=404, detail="Jardin introuvable")
    return g


@router.put("/gardens/{id}", response_model=Garden)
def update_garden(id: int, data: Garden, session: Session = Depends(get_session)):
    g = session.get(Garden, id)
    if not g:
        raise HTTPException(status_code=404, detail="Jardin introuvable")
    for k, v in data.model_dump(exclude_unset=True, exclude={"id", "created_at"}).items():
        setattr(g, k, v)
    g.updated_at = datetime.now(timezone.utc)
    session.add(g)
    session.commit()
    session.refresh(g)
    return g


@router.delete("/gardens/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_garden(id: int, session: Session = Depends(get_session)):
    g = session.get(Garden, id)
    if not g:
        raise HTTPException(status_code=404, detail="Jardin introuvable")
    session.delete(g)
    session.commit()


@router.get("/gardens/{id}/cells", response_model=List[GardenCell])
def list_cells(id: int, session: Session = Depends(get_session)):
    return session.exec(select(GardenCell).where(GardenCell.garden_id == id)).all()


@router.post("/gardens/{id}/cells", response_model=GardenCell, status_code=status.HTTP_201_CREATED)
def create_cell(id: int, cell: GardenCell, session: Session = Depends(get_session)):
    cell.garden_id = id
    session.add(cell)
    session.commit()
    session.refresh(cell)
    return cell


@router.get("/gardens/{id}/measurements", response_model=List[Measurement])
def list_measurements(id: int, session: Session = Depends(get_session)):
    return session.exec(select(Measurement).where(Measurement.garden_id == id)).all()


@router.post("/gardens/{id}/measurements", response_model=Measurement, status_code=status.HTTP_201_CREATED)
def create_measurement(id: int, m: Measurement, session: Session = Depends(get_session)):
    m.garden_id = id
    session.add(m)
    session.commit()
    session.refresh(m)
    return m
