<?php

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

class ActivityInjuryEvent extends MonthEndEvent
{
    public function getMinAge()
    {
        return 12;
    }

    public function getMinParticipants()
    {
        return 2;
    }

    public function getMaxParticipants()
    {
        return 5;
    }

    public function getCost()
    {
        return 30;
    }

    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 (count($eligible) < $this->getMinParticipants()) {
            return;
        }

        // Randomly select 2-5 participants
        shuffle($eligible);
        $count = rand($this->getMinParticipants(), min($this->getMaxParticipants(), count($eligible)));
        $participants = array_slice($eligible, 0, $count);

        $participantNames = array_map(fn($k) => $k->getName(), $participants);

        $activities = [
            'gathering expedition',
            'hunting trip',
            'fishing outing',
            'water crossing',
            'climbing exercise',
        ];

        $activity = $activities[array_rand($activities)];

        $injuryTypes = ['smallCut', 'sunburn', 'brokenLeg'];

        // Randomly injure 1-2 participants
        $injuredCount = rand(1, min(2, count($participants)));
        for ($i = 0; $i < $injuredCount; $i++) {
            $injured = $participants[array_rand($participants)];
            $injuryType = $injuryTypes[array_rand($injuryTypes)];
            $injured->addInjury($injuryType);
            $injured->save();
        }

        $namesStr = implode(' and ', $participantNames);

        $variations = [
            "During a {$activity}, {$namesStr} had a rough time, with some coming away injured.",
            "Things went awry during a {$activity}. Not all of the group made it back unscathed.",
            "A {$activity} was more treacherous than expected, and several members returned hurt.",
            "Accidents happened during a {$activity}, leaving some of the participants bruised.",
            "A {$activity} was poorly executed, resulting in injuries for some participants.",
            "{$namesStr} engaged in a {$activity} that did not go as planned, with injuries sustained.",
            "A {$activity} left several of the participants nursing wounds they received.",
        ];

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