from __future__ import annotations from typing import Any from .growth_simulation import CurrentFarmChartSimulator, GrowthSimulationError def build_yield_prediction_payload(*, farm_uuid: str, plant_name: str | None = None) -> dict[str, Any]: simulator = CurrentFarmChartSimulator() result = simulator.simulate(farm_uuid=farm_uuid, plant_name=plant_name) yield_estimate = float((result.get("metrics") or {}).get("yield_estimate") or 0.0) predicted_yield_tons = round(max(yield_estimate / 1000.0, 0.0), 2) return { "farm_uuid": farm_uuid, "plant_name": result.get("plant_name"), "predictedYieldTons": predicted_yield_tons, "predictedYieldRaw": round(yield_estimate, 2), "unit": "تن", "sourceUnit": "kg/ha", "simulationEngine": result.get("engine"), "simulationModel": result.get("model_name"), "scenarioId": result.get("scenario_id"), "simulationWarning": result.get("simulation_warning"), "supportingMetrics": result.get("metrics") or {}, } class YieldPredictionService: def get_yield_prediction(self, *, farm_uuid: str, plant_name: str | None = None) -> dict[str, Any]: try: return build_yield_prediction_payload(farm_uuid=farm_uuid, plant_name=plant_name) except GrowthSimulationError: raise