from datetime import datetime, timedelta
from .profiles import CROP_PROFILES

class CropScheduleEngine:
    """
    Generates watering, fertiliser, and inspection tasks
    for 30 days based on crop type.
    """

    def __init__(self, crop_type, planting_date):
        self.crop_type = crop_type.lower().strip()
        self.planting_date = datetime.strptime(planting_date, "%Y-%m-%d")

        if self.crop_type not in CROP_PROFILES:
            raise ValueError(f"Unsupported crop type: {crop_type}")

        self.profile = CROP_PROFILES[self.crop_type]

    def generate_schedule(self):
        tasks = []

        watering_days = self.profile["watering_days"]
        fertiliser_days = self.profile["fertiliser_days"]
        inspection_days = self.profile["inspection_days"]

        for day in range(1, 31):
            date = self.planting_date + timedelta(days=day)
            date_str = date.strftime("%Y-%m-%d")

            if day % watering_days == 0:
                tasks.append({
                    "taskType": "watering",
                    "dueDate": date_str,
                    "notes": self.profile["notes"]
                })

            if day % fertiliser_days == 0:
                tasks.append({
                    "taskType": "fertiliser",
                    "dueDate": date_str,
                    "notes": "Apply appropriate organic fertiliser"
                })

            if day % inspection_days == 0:
                tasks.append({
                    "taskType": "inspection",
                    "dueDate": date_str,
                    "notes": "General health inspection"
                })

        return tasks
