<?php

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

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

    public function getMinParticipants()
    {
        return 0;
    }

    public function getMaxParticipants()
    {
        return 0;
    }

    public function getCost()
    {
        return 50;
    }

    public function isAvailable()
    {
        if ($this->tribe->getCurrentSeason() !== 'summer') {
            return false;
        }

        return empty($this->tribe->dataStore->heatWaveActive);
    }

    private function getMonthsUntilNextSeason()
    {
        $monthOfYear = (($this->tribe->getCurrentMonth() - 1) % 12) + 1;
        $monthInSeason = (($monthOfYear - 1) % 3) + 1;
        return max(1, 3 - $monthInSeason + 1);
    }

    public function execute()
    {
        if (!isset($this->tribe->dataStore)) {
            $this->tribe->dataStore = new stdClass();
        }

        $this->tribe->dataStore->heatWaveActive = rand(1, $this->getMonthsUntilNextSeason());

        foreach ($this->tribe->getKobolds() as $kobold) {
            $vitality = (int) ($kobold->getStat('vitality') ?? 0);
            $fatigue = (int) ceil($vitality * 0.25);
            if ($fatigue > 0) {
                $kobold->addFatigue($fatigue);
            }
        }

        $variations = [
            "A punishing heat wave settled over the camp and left everyone drained. The tribe moved slowly under the sun, and their fatigue deepened.",
            "The air shimmered with heat for days. Kobolds labored through it, and the whole tribe felt the heavy exhaustion.",
            "Summer's worst heat arrived, and even simple tasks became burdens. The tribe endured the wave with sore limbs and weary eyes."
        ];

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