class FarmingSystem {
    constructor(scene) {
        this.scene = scene;
        this.growthTimer = 0;
        this.growthTickRate = 5000; // Check growth every 5 seconds (real time)
        // Or better: based on TimeSystem days? 
        // For fast testing: rapid growth.
    }

    // Called by InteractionSystem
    interact(gridX, gridY, toolType) {
        const terrain = this.scene.terrainSystem;
        const tile = terrain.getTile(gridX, gridY);

        if (!tile) return false;

        // 1. HARVEST (Right click or just click ripe crop?)
        // Let's say if it has crop and it is ripe, harvest it regardless of tool.
        if (tile.hasCrop) {
            const crop = terrain.cropsMap.get(`${gridX},${gridY}`);
            if (crop && crop.stage === 4) {
                this.harvest(gridX, gridY);
                return true;
            }
        }

        // 2. TILLING (Requires Hoe)
        if (toolType === 'hoe') {
            if (tile.type === 'grass' || tile.type === 'dirt') {
                if (!tile.hasDecoration && !tile.hasCrop) {
                    console.log('🚜 Tilling soil...');
                    terrain.setTileType(gridX, gridY, 'farmland');
                    // Play sound
                    return true;
                }
            }
        }

        // 3. PLANTING (Requires Seeds)
        if (toolType === 'seeds') {
            if (tile.type === 'farmland' && !tile.hasCrop && !tile.hasDecoration) {
                console.log('🌱 Planting seeds...');
                this.plant(gridX, gridY);
                return true; // Consume seed logic handled by caller?
            }
        }

        return false;
    }

    plant(x, y) {
        const terrain = this.scene.terrainSystem;
        const cropData = {
            gridX: x,
            gridY: y,
            stage: 1, // Seeds
            type: 'wheat', // Default for now
            timer: 0,
            maxTime: 10 // Seconds per stage?
        };
        terrain.addCrop(x, y, cropData);
    }

    harvest(x, y) {
        const terrain = this.scene.terrainSystem;
        console.log('🌾 Harvesting!');

        // Spawn loot
        if (this.scene.interactionSystem) {
            this.scene.interactionSystem.spawnLoot(x, y, 'wheat');
            this.scene.interactionSystem.spawnLoot(x, y, 'seeds'); // Return seeds
            // 50% chance for extra seeds
            if (Math.random() > 0.5) this.scene.interactionSystem.spawnLoot(x, y, 'seeds');
        }

        // Remove crop
        terrain.removeCrop(x, y);

        // Revert to dirt? Or keep farmland? Usually keeps farmland.
    }

    update(delta) {
        // Growth Logic
        // Iterate all crops? Expensive?
        // Better: Random tick like Minecraft or list iteration.
        // Since we have cropsMap, iteration is easy.

        // Only run every 1 second (1000ms) to save PERF
        this.growthTimer += delta;
        if (this.growthTimer < 1000) return;
        const secondsPassed = this.growthTimer / 1000;
        this.growthTimer = 0;

        const terrain = this.scene.terrainSystem;
        if (!terrain) return;

        for (const [key, crop] of terrain.cropsMap) {
            if (crop.stage < 4) {
                crop.timer += secondsPassed;

                // Growth thresholds (fast for testing)
                // Stage 1 -> 2: 5s
                // Stage 2 -> 3: 10s
                // Stage 3 -> 4: 15s
                const needed = 5;

                if (crop.timer >= needed) {
                    crop.stage++;
                    crop.timer = 0;
                    terrain.updateCropVisual(crop.gridX, crop.gridY, crop.stage);
                    // Particle effect?
                }
            }
        }
    }
}
