from functools import cached_property

from django.apps import AppConfig


class IrrigationConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "irrigation"
    verbose_name = "Irrigation"
    tone_file = "config/tones/irrigation_tone.txt"

    @cached_property
    def optimizer_defaults(self):
        return {
            "simulation_model": "Wofost81_NWLP_CWB_CNB",
            "validity_days": 3,
            "minimum_event_mm": 4.0,
            "significant_rain_threshold_mm": 4.0,
            "stage_targets": {
                "initial": 65,
                "vegetative": 70,
                "flowering": 75,
                "fruiting": 68,
            },
            "strategy_profiles": [
                {
                    "code": "conservative",
                    "label": "آبیاری محافظه کارانه",
                    "multiplier": 0.82,
                    "frequency_factor": 0.85,
                    "event_count": 2,
                },
                {
                    "code": "balanced",
                    "label": "آبیاری متعادل",
                    "multiplier": 1.0,
                    "frequency_factor": 1.0,
                    "event_count": 3,
                },
                {
                    "code": "protective",
                    "label": "آبیاری حمایتی",
                    "multiplier": 1.12,
                    "frequency_factor": 1.2,
                    "event_count": 4,
                },
            ],
        }

    def get_optimizer_defaults(self):
        return self.optimizer_defaults

    @cached_property
    def water_stress_service(self):
        from .indicators import WaterStressService

        return WaterStressService()

    def get_water_stress_service(self):
        return self.water_stress_service

    @cached_property
    def free_text_plan_parser_service(self):
        from rag.services.irrigation_plan_parser import IrrigationPlanParserService

        return IrrigationPlanParserService()

    def get_free_text_plan_parser_service(self):
        return self.free_text_plan_parser_service
