"""WESP-shaped warehouse API (/api/sklad) for consumption.html.""" from __future__ import annotations from fastapi import APIRouter, Depends, HTTPException, Query, status from pydantic import BaseModel, Field from app.modules.sync.tenant import TenantContext, require_enterprise_zootech from app.modules.zootech import wesp_sklad_service as sklad_service router = APIRouter() def _require_ent(enterprise_id: str, tenant: TenantContext) -> None: if tenant.enterprise_id != enterprise_id: raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="ENTERPRISE_FORBIDDEN") class SkladAddBody(BaseModel): component_id: str class SkladUpdateBody(BaseModel): total_kg: float = Field(ge=0) inflow_kg: float = Field(default=0, ge=0) class SkladMoveBody(BaseModel): to_index: int = Field(ge=0) @router.get("/sklad") def list_sklad_wesp( enterprise_id: str = Query(...), date_from: str | None = Query(None), date_to: str | None = Query(None), tenant: TenantContext = Depends(require_enterprise_zootech), ): _require_ent(enterprise_id, tenant) return sklad_service.list_sklad_items(enterprise_id, date_from=date_from, date_to=date_to) @router.post("/sklad/add") def add_sklad_wesp( body: SkladAddBody, enterprise_id: str = Query(...), tenant: TenantContext = Depends(require_enterprise_zootech), ): _require_ent(enterprise_id, tenant) return sklad_service.add_sklad_item(enterprise_id, body.component_id.strip()) @router.post("/sklad/{component_id}") def update_sklad_wesp( component_id: str, body: SkladUpdateBody, enterprise_id: str = Query(...), tenant: TenantContext = Depends(require_enterprise_zootech), ): _require_ent(enterprise_id, tenant) return sklad_service.update_sklad_item( enterprise_id, component_id, total_kg=body.total_kg, inflow_kg=body.inflow_kg, ) @router.delete("/sklad/{component_id}") def remove_sklad_wesp( component_id: str, enterprise_id: str = Query(...), tenant: TenantContext = Depends(require_enterprise_zootech), ): _require_ent(enterprise_id, tenant) return sklad_service.remove_sklad_item(enterprise_id, component_id) @router.put("/sklad/{component_id}/move") def move_sklad_wesp( component_id: str, body: SkladMoveBody, enterprise_id: str = Query(...), tenant: TenantContext = Depends(require_enterprise_zootech), ): _require_ent(enterprise_id, tenant) return sklad_service.move_sklad_item(enterprise_id, component_id, body.to_index) @router.get("/sklad/{component_id}/chart") def sklad_chart_wesp( component_id: str, enterprise_id: str = Query(...), date_from: str | None = Query(None), date_to: str | None = Query(None), tenant: TenantContext = Depends(require_enterprise_zootech), ): _require_ent(enterprise_id, tenant) return sklad_service.sklad_chart_points( enterprise_id, component_id, date_from=date_from, date_to=date_to, )