<?php

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

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

    public function getMinParticipants()
    {
        return 2;
    }

    public function getMaxParticipants()
    {
        return 150;
    }

    public function getCost()
    {
        return 35;
    }

    public function isAvailable()
    {
        $adults = array_filter($this->tribe->getKobolds(), function ($kobold) {
            return $kobold->getAgeCategory() === 'adult';
        });

        if (empty($adults)) {
            return false;
        }

        $children = array_filter($this->tribe->getKobolds(), function ($kobold) {
            return $kobold->getAgeCategory() === 'baby' || $kobold->getAgeCategory() === 'young';
        });

        return !empty($children);
    }

    private function ensureLikes($kobold)
    {
        if (!isset($kobold->dataStore)) {
            $kobold->dataStore = new stdClass();
        }
        if (!isset($kobold->dataStore->likes) || !is_array($kobold->dataStore->likes)) {
            $kobold->dataStore->likes = [];
        }
    }

    public function execute()
    {
        $adults = array_values(array_filter($this->tribe->getKobolds(), function ($kobold) {
            return $kobold->getAgeCategory() === 'adult';
        }));

        $children = array_values(array_filter($this->tribe->getKobolds(), function ($kobold) {
            return $kobold->getAgeCategory() === 'baby' || $kobold->getAgeCategory() === 'young';
        }));

        if (empty($adults) || empty($children)) {
            return;
        }

        $caretaker = $adults[array_rand($adults)];
        shuffle($children);
        $count = rand(1, min(3, count($children)));
        $wards = array_slice($children, 0, $count);

        $this->ensureLikes($caretaker);
        foreach ($wards as $ward) {
            $this->ensureLikes($ward);
            $caretaker->dataStore->likes[$ward->getId()] = ($caretaker->dataStore->likes[$ward->getId()] ?? 0) + 2;
            $ward->dataStore->likes[$caretaker->getId()] = ($ward->dataStore->likes[$caretaker->getId()] ?? 0) + 2;
        }

        $wardNames = array_map(fn($k) => $k->getName(), $wards);
        $namesText = implode(', ', $wardNames);

        $variations = [
            "{$caretaker->getName()} spent the month watching over {$namesText}, tending to their needs and sharing stories. The time together warmed their bond and left the young ones more trusting.",
            "With the camp busy, {$caretaker->getName()} took care of {$namesText}. The simple acts of care grew mutual affection between them.",
            "{$caretaker->getName()} guided {$namesText} through chores and play. By month's end, the tribe noticed their growing fondness."
        ];

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

        $caretaker->save();
        foreach ($wards as $ward) {
            $ward->save();
        }
    }
}
