<?php

require_once __DIR__ . '/../../src/Events/MonthEndEvent.php';
require_once __DIR__ . '/../../src/Classes/Kobold.php';

class InjuryRecoveryEvent extends MonthEndEvent
{
    public function getMinAge()
    {
        return 0;
    }

    public function getMinParticipants()
    {
        return 1;
    }

    public function getMaxParticipants()
    {
        return 1;
    }

    public function getCost()
    {
        return 40;
    }

    public function isAvailable()
    {
        $eligible = array_filter($this->tribe->getAliveKobolds(), function ($k) {
            $injuries = $k->getInjuries() ?? [];
            foreach ($injuries as $inj) {
                $category = $inj['category'] ?? null;
                // Only these categories can heal naturally
                if (in_array($category, ['cut', 'bruise', 'burn'])) {
                    return true;
                }
            }
            return false;
        });

        $eligible = array_values($eligible);
        return !empty($eligible);
    }

    public function execute()
    {
        $this->story = '';

        // Find a kobold with injuries that can heal naturally
        $eligible = array_filter($this->tribe->getAliveKobolds(), function ($k) {
            $injuries = $k->getInjuries() ?? [];
            foreach ($injuries as $inj) {
                $category = $inj['category'] ?? null;
                // Only these categories can heal naturally
                if (in_array($category, ['cut', 'bruise', 'burn'])) {
                    return true;
                }
            }
            return false;
        });

        $eligible = array_values($eligible);

        if (empty($eligible)) {
            return;
        }

        $kobold = $eligible[array_rand($eligible)];
        $injuries = $kobold->getInjuries() ?? [];

        // Find first healable injury
        $healableInjuries = [];
        foreach ($injuries as $inj) {
            $category = $inj['category'] ?? null;
            if (in_array($category, ['cut', 'bruise', 'burn'])) {
                $healableInjuries[] = $inj;
            }
        }

        if (empty($healableInjuries)) {
            return;
        }

        $toHeal = $healableInjuries[array_rand($healableInjuries)];
        $kobold->removeInjury($toHeal['id']);

        $nameA = $kobold->getName();
        $injuryName = $toHeal['injury_type'] ?? $toHeal['injuryType'] ?? 'injury';

        $recoveryVariations = [
            "{$nameA} has healed from the {$injuryName} and is feeling much better.",
            "Time and rest worked wonders for {$nameA}, who has recovered from the {$injuryName}.",
            "{$nameA}'s {$injuryName} finally healed after a month of careful rest.",
            "The tribe is relieved to see {$nameA}'s {$injuryName} finally resolved.",
            "{$nameA} is back to full strength, the {$injuryName} now a fading memory.",
            "A period of quiet recovery allowed {$nameA} to heal from the {$injuryName}.",
            "{$nameA} has made a full recovery from the {$injuryName}.",
        ];

        $this->story = $recoveryVariations[array_rand($recoveryVariations)];

        $kobold->save();
    }
}
