<?php

namespace TribeGen\Events;

class ExplorationEvent extends Event
{
    public function execute()
    {
        $this->story = "The exploration party ventured forth into uncharted territory...\n\n";

        $kobolds = $this->tribe->getAliveKobolds();

        if (empty($kobolds)) {
            $this->story .= "But there were no kobolds to send exploring!";
            return;
        }

        // Randomly select 1-3 explorers
        $explorerCount = rand(1, min(3, count($kobolds)));
        $explorers = array_slice(array_values($kobolds), 0, $explorerCount);
        
        $this->story .= "A group of " . count($explorers) . " intrepid explorers set out:\n";
        foreach ($explorers as $explorer) {
            $this->story .= "- " . $explorer->getName() . " (Intelligence: " . $this->statDisplay($explorer->getStat('intelligence')) . ")\n";
        }
        $this->story .= "\n";

        // Chance to find food (50% base)
        $foodFound = rand(1, 100) <= 50;
        if ($foodFound) {
            $baseFood = rand(5, 15);
            // Intelligence affects food discovery - smarter kobolds find more
            $avgIntelligence = array_sum(array_map(function($k) { return $k->getStat('intelligence'); }, $explorers)) / count($explorers);
            $foodBonus = intval($avgIntelligence / 20); // Max +5 food from intelligence
            $totalFood = $baseFood + $foodBonus;
            
            $this->tribe->addFood($totalFood);
            $this->story .= "Success! The explorers discovered edible plants and small game.\n";
            $this->story .= "The tribe gained " . $totalFood . " units of food.\n\n";
        } else {
            $this->story .= "The terrain was barren. No food was found.\n\n";
        }

        // Chance for injury (35% - someone trips on loose stone)
        $injuryChance = rand(1, 100) <= 35;
        if ($injuryChance && count($explorers) > 0) {
            // Vitality affects injury chance - higher vitality = less likely to be injured
            $avgVitality = array_sum(array_map(function($k) { return $k->getStat('vitality'); }, $explorers)) / count($explorers);
            $injuryProbability = (100 - $avgVitality) / 100;

            if (rand(1, 100) / 100 <= $injuryProbability) {
                // Select a random explorer to get injured
                $injuredKobold = $explorers[array_rand($explorers)];
                
                $this->story .= "Disaster! " . $injuredKobold->getName() . " tripped over a loose stone!\n";
                
                // Create injury array (since we're in file-based storage)
                $injuries = $injuredKobold->getInjuries();
                $newInjury = [
                    'id' => uniqid('inj_'),
                    'kobold_id' => $injuredKobold->getId(),
                    'injury_type' => 'twisted_ankle',
                    'category' => 'wound',
                    'severity' => rand(2, 5),
                    'created_at' => date('Y-m-d H:i:s')
                ];
                $injuries[] = $newInjury;
                $injuredKobold->setInjuries($injuries);
                
                $this->story .= $injuredKobold->getName() . " suffered a twisted ankle (severity: " . $newInjury['severity'] . "/10).\n";
            }
        }

        $this->story .= "\nThe exploration party returned to the tribe with their findings.";

        return $this->story;
    }

    /**
     * Convert internal stat value to display text
     */
    private function statDisplay($stat)
    {
        if ($stat <= 10) return "terrible";
        if ($stat <= 20) return "bad";
        if ($stat <= 30) return "unskilled";
        if ($stat <= 40) return "decent";
        if ($stat <= 50) return "good";
        if ($stat <= 60) return "excellent";
        if ($stat <= 70) return "fantastic";
        if ($stat <= 80) return "master";
        if ($stat <= 90) return "divine";
        return "godlike";
    }
}
