class StatsSystem {
    constructor(scene) {
        this.scene = scene;

        // Stats
        this.health = 100;
        this.maxHealth = 100;

        this.hunger = 100; // 100 = full
        this.maxHunger = 100;

        this.thirst = 100; // 100 = not thirsty
        this.maxThirst = 100;

        // Decay rates (per second)
        this.hungerDecay = 0.5; // Pade na 0 v 200s (cca 3 min)
        this.thirstDecay = 0.8; // Pade na 0 v 125s (cca 2 min)

        this.damageTickTimer = 0;

        // Friendship System (Hearts ❤️)
        this.friendship = {
            merchant: 0,
            zombie: 0,
            villager: 0
        };
    }

    update(delta) {
        const seconds = delta / 1000;

        // Decay
        if (this.hunger > 0) {
            this.hunger -= this.hungerDecay * seconds;
        }
        if (this.thirst > 0) {
            this.thirst -= this.thirstDecay * seconds;
        }

        // Clamp values
        this.hunger = Math.max(0, this.hunger);
        this.thirst = Math.max(0, this.thirst);

        // Starvation / Dehydration logic
        if (this.hunger <= 0 || this.thirst <= 0) {
            this.damageTickTimer += delta;
            if (this.damageTickTimer >= 1000) { // Vsako sekundo damage
                this.damageTickTimer = 0;
                this.takeDamage(5); // 5 DMG na sekundo če si lačen/žejen

                // Shake camera effect za opozorilo
                this.scene.cameras.main.shake(100, 0.005);
            }
        } else {
            this.damageTickTimer = 0;

            // Natural regeneration if full
            if (this.hunger > 80 && this.thirst > 80 && this.health < this.maxHealth) {
                this.health += 1 * seconds;
            }
        }

        this.health = Math.min(this.health, this.maxHealth);

        this.updateUI();
    }

    takeDamage(amount) {
        this.health -= amount;
        if (this.health <= 0) {
            this.health = 0;
            this.die();
        }
    }

    eat(amount) {
        this.hunger += amount;
        this.hunger = Math.min(this.hunger, this.maxHunger);
    }

    drink(amount) {
        this.thirst += amount;
        this.thirst = Math.min(this.thirst, this.maxThirst);
    }

    die() {
        console.log('💀 Player died!');

        // Trigger Player Animation
        if (this.scene.player) {
            this.scene.player.dieAnimation();
        }

        // Show Notification & Overlay in UI Scene
        const uiScene = this.scene.scene.get('UIScene');
        if (uiScene) {
            // Full screen overlay using scale dimensions
            const width = uiScene.scale.width;
            const height = uiScene.scale.height;

            const bg = uiScene.add.rectangle(width / 2, height / 2, width, height, 0x000000, 0.8);

            const txt = uiScene.add.text(width / 2, height / 2, 'YOU DIED', {
                fontSize: '64px', color: '#ff0000', fontStyle: 'bold'
            }).setOrigin(0.5);

            // Wait and Respawn
            uiScene.time.delayedCall(3000, () => {
                if (bg) bg.destroy();
                if (txt) txt.destroy();

                // Reset Stats
                this.health = 100;
                this.hunger = 100;
                this.thirst = 100;
                this.updateUI();

                // Reset Player
                if (this.scene.player) {
                    this.scene.player.respawn();
                }
            });
        }
    }

    updateUI() {
        const uiScene = this.scene.scene.get('UIScene');
        if (uiScene) {
            if (uiScene.healthBar) uiScene.setBarValue(uiScene.healthBar, this.health);
            if (uiScene.hungerBar) uiScene.setBarValue(uiScene.hungerBar, this.hunger);
            if (uiScene.thirstBar) uiScene.setBarValue(uiScene.thirstBar, this.thirst);
        }
    }

    // Friendship System
    addFriendship(npcType, amount) {
        if (this.friendship[npcType] !== undefined) {
            this.friendship[npcType] += amount;
            console.log(`❤️ +${amount} Friendship with ${npcType} (Total: ${this.friendship[npcType]})`);
        }
    }

    getFriendship(npcType) {
        return this.friendship[npcType] || 0;
    }

    setFriendship(npcType, amount) {
        if (this.friendship[npcType] !== undefined) {
            this.friendship[npcType] = amount;
        }
    }
}
