<?php

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

class WalkDiscoveryEvent extends MonthEndEvent
{
    public function getMinAge()
    {
        return 13;
    }

    public function getMinParticipants()
    {
        return 1;
    }

    public function getMaxParticipants()
    {
        return 1;
    }

    public function getCost()
    {
        return 40;
    }

    public function isAvailable()
    {
        return true;
    }

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

        $eligible = array_filter($this->tribe->getAliveKobolds(), fn($k) => $k->getAge() >= $this->getMinAge());
        $eligible = array_values($eligible);

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

        $kobold = $eligible[array_rand($eligible)];

        // Determine what was found
        $roll = rand(1, 100);

        $nameA = $kobold->getName();

        if ($roll <= 60) {
            // Food discovery
            $foodAmount = rand(3, 8);

            $reason = $this->tribe->modifyFoodGain($foodAmount);
            if ($foodAmount < 1) {
                $foodAmount = 1;
            }
            $this->tribe->addFood($foodAmount);

            $foodStr = $this->tribe->foodTimeStr($foodAmount);

            $foodVariations = [
                "{$nameA} found some edible plants during a walk and brought back {$foodStr} worth of food.",
                "While exploring, {$nameA} discovered a cache of berries and returned with {$foodStr} worth of food.",
                "{$nameA}'s keen eye spotted food sources, adding {$foodStr} worth of food to the tribe's stores.",
                "A fortunate walk led {$nameA} to gather {$foodStr} worth of food for the tribe.",
                "{$nameA} returned from a walk with {$foodStr} worth of food, a welcome find.",
                "During a solitary walk, {$nameA} found edible resources and collected {$foodStr} worth of food.",
                "{$nameA} stumbled upon a food source during a walk, bringing back {$foodStr} worth of food.",
            ];

            $this->story = $foodVariations[array_rand($foodVariations)];
            if ($reason === 'draught') {
                $this->story .= " The drought kept the find small.";
            } elseif ($reason === 'heatWave') {
                $this->story .= " The heat kept the find small.";
            }
        } else {
            // Item discovery
            $items = [
                ['name' => 'bone-setting', 'displayName' => 'bone-setting kit'],
                ['name' => 'healing-herbs', 'displayName' => 'healing herbs'],
                ['name' => 'book', 'displayName' => 'old book'],
            ];

            $item = $items[array_rand($items)];

            // Load item template
            $itemData = GameHelper::loadItem($item['name']);
            if ($itemData) {
                $this->tribe->addToStorage($itemData);

                $itemVariations = [
                    "{$nameA} discovered an old {$item['displayName']} during a walk.",
                    "While wandering, {$nameA} found a {$item['displayName']} and brought it back.",
                    "{$nameA}'s sharp eyes spotted a {$item['displayName']} lying in the wilderness.",
                    "A lucky find—{$nameA} returned with a {$item['displayName']}.",
                    "{$nameA} came back from a walk carrying a {$item['displayName']}.",
                    "The tribe is pleased that {$nameA} found a {$item['displayName']} on a recent walk.",
                    "{$nameA} stumbled upon a {$item['displayName']} and wisely brought it to the tribe.",
                ];

                $this->story = $itemVariations[array_rand($itemVariations)];
            } else {
                // Fallback if item loading fails
                $this->story = "{$nameA} returned from a walk with interesting findings.";
            }
        }
    }
}
