from __future__ import annotations

from unittest.mock import patch

from django.test import TestCase, override_settings
from rest_framework.test import APIClient


@override_settings(ROOT_URLCONF="irrigation.urls")
class IrrigationRecommendApiTests(TestCase):
    def setUp(self):
        self.client = APIClient()

    @patch("rag.services.irrigation.get_irrigation_recommendation")
    def test_recommend_api_returns_water_balance(self, mock_get_irrigation_recommendation):
        mock_get_irrigation_recommendation.return_value = {
            "plan": {
                "frequencyPerWeek": 4,
                "durationMinutes": 38,
                "bestTimeOfDay": "05:30 تا 08:00 صبح",
                "moistureLevel": 72,
                "warning": "در ساعات گرم روز آبیاری انجام نشود",
            },
            "water_balance": {
                "active_kc": 0.93,
                "crop_profile": {
                    "kc_initial": 0.55,
                    "kc_mid": 1.05,
                    "kc_end": 0.78,
                },
                "daily": [
                    {
                        "forecast_date": "2025-02-12",
                        "et0_mm": 5.4,
                        "etc_mm": 4.9,
                        "effective_rainfall_mm": 0,
                        "gross_irrigation_mm": 17,
                        "irrigation_timing": "05:30 - 07:00",
                    }
                ],
            },
            "timeline": [],
            "sections": [],
            "simulation_optimizer": {"engine": "crop_simulation_heuristic"},
            "selected_irrigation_method": {"name": "آبیاری قطره‌ای"},
        }

        response = self.client.post(
            "/recommend/",
            data={
                "farm_uuid": "11111111-1111-1111-1111-111111111111",
                "plant_name": "گوجه‌فرنگی",
                "growth_stage": "گلدهی",
                "irrigation_method_name": "آبیاری قطره‌ای",
            },
            format="json",
        )

        self.assertEqual(response.status_code, 200)
        data = response.json()["data"]
        self.assertIn("water_balance", data)
        self.assertEqual(data["water_balance"]["active_kc"], 0.93)
        self.assertNotIn("simulation_optimizer", data)
        self.assertNotIn("selected_irrigation_method", data)
