"""Layout router — thin: parse -> service -> schema.

Serves the layout-mode editor payload and the plant-position save endpoint
(shared by the area-detail sketch drag UI and layout mode). All behavior
lives in services/layout.py. Sync def per standards (DB routes).
"""
from __future__ import annotations

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from ..db import get_db
from ..deps import current_garden_id
from ..schemas.areas import LayoutOut, PositionsSaveOut, PositionsSaveRequest
from ..services import layout as layout_svc

router = APIRouter(prefix="/api", tags=["layout"])


@router.get("/areas/{area_id}/layout", response_model=LayoutOut)
def area_layout(
    area_id: int,
    garden_id: int = Depends(current_garden_id),
    db: Session = Depends(get_db),
):
    """Layout-mode payload: axis-aligned geometry (compass_rot carries the
    real orientation), placed icons (strict positions, no auto-fill),
    per-group inventory tiles, and the global Unplanted/Idea pool tiles.
    Errors: ``area_not_found`` (404), ``area_no_dimensions`` (400)."""
    return layout_svc.get_layout_payload(db, garden_id, area_id)


@router.post("/plants/{plant_id}/positions", response_model=PositionsSaveOut)
def plant_set_positions(
    plant_id: int,
    body: PositionsSaveRequest,
    garden_id: int = Depends(current_garden_id),
    db: Session = Depends(get_db),
):
    """Save a plant_group's icon positions (feet, rect-local). Invalid
    entries skipped, coordinates clamped to the area bounds, list capped at
    the group's quantity — exact v1 semantics."""
    return layout_svc.save_plant_positions(db, garden_id, plant_id, body.positions)
