from functools import cached_property

from django.apps import AppConfig


class FertilizationConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "fertilization"
    verbose_name = "Fertilization"
    tone_file = "config/tones/fertilization_tone.txt"

    @cached_property
    def optimizer_defaults(self):
        return {
            "simulation_model": "Wofost81_NWLP_CWB_CNB",
            "validity_days": 7,
            "default_application_interval_days": 14,
            "rain_delay_threshold_mm": 3.0,
            "stage_targets": {
                "initial": {
                    "n": 28.0,
                    "p": 20.0,
                    "k": 24.0,
                    "formula": "10-52-10",
                    "application_method": "استارتر نواری یا همراه آب آبیاری",
                    "timing": "همزمان با استقرار بوته و در ساعات خنک روز",
                    "application_interval_days": 10,
                },
                "vegetative": {
                    "n": 55.0,
                    "p": 28.0,
                    "k": 42.0,
                    "formula": "20-20-20",
                    "application_method": "کودآبیاری یا سرک خاکی سبک",
                    "timing": "صبح زود و ترجیحا قبل از نوبت آبیاری",
                    "application_interval_days": 12,
                },
                "flowering": {
                    "n": 42.0,
                    "p": 32.0,
                    "k": 58.0,
                    "formula": "15-10-30",
                    "application_method": "کودآبیاری یا محلول پاشی سبک",
                    "timing": "صبح زود و دور از تنش گرمایی ظهر",
                    "application_interval_days": 14,
                },
                "fruiting": {
                    "n": 35.0,
                    "p": 24.0,
                    "k": 68.0,
                    "formula": "12-12-36",
                    "application_method": "کودآبیاری با تاکید بر پتاس",
                    "timing": "صبح زود یا نزدیک غروب",
                    "application_interval_days": 10,
                },
            },
            "strategy_profiles": [
                {
                    "code": "maintenance",
                    "label": "تغذیه نگهدارنده",
                    "multiplier": 0.8,
                    "focus": "پایه متعادل",
                    "application_method": "کودآبیاری",
                    "formula_override": "",
                },
                {
                    "code": "balanced",
                    "label": "تغذیه متعادل",
                    "multiplier": 1.0,
                    "focus": "ازت فسفر پتاس متعادل",
                    "application_method": "کودآبیاری",
                    "formula_override": "",
                },
                {
                    "code": "corrective",
                    "label": "تغذیه اصلاحی",
                    "multiplier": 1.2,
                    "focus": "ازت و پتاس اصلاحی",
                    "application_method": "کودآبیاری",
                    "formula_override": "",
                },
            ],
        }

    def get_optimizer_defaults(self):
        return self.optimizer_defaults

    @cached_property
    def free_text_plan_parser_service(self):
        from rag.services.fertilization_plan_parser import FertilizationPlanParserService

        return FertilizationPlanParserService()

    def get_free_text_plan_parser_service(self):
        return self.free_text_plan_parser_service
