<?php

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

class SocialEvent extends MonthEndEvent
{
    public function getMinAge()
    {
        return 5;
    }

    public function getMinParticipants()
    {
        return 2;
    }

    public function getMaxParticipants()
    {
        return 2;
    }

    public function getCost()
    {
        return 50;
    }

    public function isAvailable()
    {
        return true;
    }

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

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

        if (count($eligibleKobolds) < 2) {
            return;
        }

        // Select two random kobolds
        $shuffled = $eligibleKobolds;
        shuffle($shuffled);
        $koboldA = $shuffled[0];
        $koboldB = $shuffled[1];

        // Initialize dataStore->likes if not present
        if (!isset($koboldA->dataStore)) {
            $koboldA->dataStore = new stdClass();
        }
        if (!isset($koboldA->dataStore->likes)) {
            $koboldA->dataStore->likes = [];
        }

        if (!isset($koboldB->dataStore)) {
            $koboldB->dataStore = new stdClass();
        }
        if (!isset($koboldB->dataStore->likes)) {
            $koboldB->dataStore->likes = [];
        }

        // Get current likes values (default to 0)
        $aLikesB = $koboldA->dataStore->likes[$koboldB->getId()] ?? 0;
        $bLikesA = $koboldB->dataStore->likes[$koboldA->getId()] ?? 0;

        // Roll personality checks (factoring in their feelings toward each other)
        $aSuccess = $koboldA->checkPersonality($koboldB);
        $bSuccess = $koboldB->checkPersonality($koboldA);

        $nameA = $koboldA->getName();
        $nameB = $koboldB->getName();

        if ($aSuccess && $bSuccess) {
            // Both succeed - mutual fondness grows
            $koboldA->dataStore->likes[$koboldB->getId()] = $aLikesB + 1;
            $koboldB->dataStore->likes[$koboldA->getId()] = $bLikesA + 1;

            $bothSuccessVariations = [
                "{$nameA} and {$nameB} spent a pleasant afternoon sharing stories by the fire, their bond deepening with each tale.",
                "Under the shade of an old tree, {$nameA} and {$nameB} found themselves laughing together, discovering a growing fondness for one another.",
                "{$nameA} and {$nameB} worked side by side this month, their companionship blossoming as they learned to trust each other more.",
                "A quiet understanding passed between {$nameA} and {$nameB} as they watched the sunset together, their friendship strengthening in the peaceful silence.",
                "{$nameA} and {$nameB} explored the nearby stream, skipping stones and sharing dreams, growing closer with every moment.",
                "Through shared hardship and triumph, {$nameA} and {$nameB} found their connection deepening, each grateful for the other's presence.",
                "{$nameA} taught {$nameB} a new skill this month, and in return {$nameB} shared wisdom of their own—their mutual respect blossomed.",
                "A chance encounter led {$nameA} and {$nameB} to spend the day together, discovering common interests and kindred spirits.",
                "{$nameA} and {$nameB} found comfort in each other's company during a stormy night, their bond growing stronger through the shared experience.",
                "The tribe noticed {$nameA} and {$nameB} seeking each other out more often, their friendship becoming a source of joy for both.",
                "{$nameA} and {$nameB} collaborated on a small project, their easy cooperation revealing a deepening affection between them.",
                "During a quiet moment in the bustling camp, {$nameA} and {$nameB} exchanged knowing glances, their understanding of each other growing ever richer."
            ];

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

        } elseif ($aSuccess || $bSuccess) {
            // One succeeds - one-sided fondness grows
            if ($aSuccess) {
                $koboldA->dataStore->likes[$koboldB->getId()] = $aLikesB + 1;

                $aSuccessVariations = [
                    "{$nameA} found themselves drawn to {$nameB}'s company this month, though {$nameB} seemed somewhat distracted and distant.",
                    "While {$nameB} was preoccupied with other matters, {$nameA} grew more appreciative of {$nameB}'s qualities and presence in the tribe.",
                    "{$nameA} made several attempts to connect with {$nameB}, and though the responses were lukewarm, {$nameA}'s fondness only grew.",
                    "As {$nameA} watched {$nameB} from afar, admiration bloomed—though {$nameB} remained largely unaware of the attention.",
                    "{$nameA} went out of their way to help {$nameB} with various tasks, developing a quiet affection despite {$nameB}'s apparent indifference.",
                    "Through patient observation, {$nameA} came to appreciate {$nameB}'s unique strengths, even as {$nameB} barely noticed the growing admiration.",
                    "{$nameA} found {$nameB}'s quirks increasingly endearing this month, though their interactions remained somewhat one-sided.",
                    "A series of small gestures from {$nameA} toward {$nameB} went largely unacknowledged, yet {$nameA}'s regard only deepened.",
                    "{$nameA} discovered a growing respect for how {$nameB} handled challenges, even if {$nameB} didn't reciprocate the sentiment.",
                    "Despite {$nameB}'s cool demeanor, {$nameA} saw glimpses of something admirable beneath the surface, and their fondness grew."
                ];

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

            } else {
                $koboldB->dataStore->likes[$koboldA->getId()] = $bLikesA + 1;

                $bSuccessVariations = [
                    "{$nameB} found themselves drawn to {$nameA}'s company this month, though {$nameA} seemed somewhat distracted and distant.",
                    "While {$nameA} was preoccupied with other matters, {$nameB} grew more appreciative of {$nameA}'s qualities and presence in the tribe.",
                    "{$nameB} made several attempts to connect with {$nameA}, and though the responses were lukewarm, {$nameB}'s fondness only grew.",
                    "As {$nameB} watched {$nameA} from afar, admiration bloomed—though {$nameA} remained largely unaware of the attention.",
                    "{$nameB} went out of their way to help {$nameA} with various tasks, developing a quiet affection despite {$nameA}'s apparent indifference.",
                    "Through patient observation, {$nameB} came to appreciate {$nameA}'s unique strengths, even as {$nameA} barely noticed the growing admiration.",
                    "{$nameB} found {$nameA}'s quirks increasingly endearing this month, though their interactions remained somewhat one-sided.",
                    "A series of small gestures from {$nameB} toward {$nameA} went largely unacknowledged, yet {$nameB}'s regard only deepened.",
                    "{$nameB} discovered a growing respect for how {$nameA} handled challenges, even if {$nameA} didn't reciprocate the sentiment.",
                    "Despite {$nameA}'s cool demeanor, {$nameB} saw glimpses of something admirable beneath the surface, and their fondness grew."
                ];

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

        } else {
            // Both fail - mutual dislike grows
            $koboldA->dataStore->likes[$koboldB->getId()] = $aLikesB - 1;
            $koboldB->dataStore->likes[$koboldA->getId()] = $bLikesA - 1;

            $bothFailVariations = [
                "{$nameA} and {$nameB} had a heated disagreement this month, each walking away with a worse opinion of the other.",
                "Tensions flared between {$nameA} and {$nameB} over a trivial matter, leaving both feeling resentful and bitter.",
                "What began as a simple conversation between {$nameA} and {$nameB} devolved into harsh words and hurt feelings on both sides.",
                "{$nameA} and {$nameB} clashed over different approaches to a problem, each convinced the other was being unreasonable.",
                "A misunderstanding between {$nameA} and {$nameB} escalated into mutual accusations, souring their relationship further.",
                "{$nameA} and {$nameB} found themselves grating on each other's nerves this month, each finding fault in the other's behavior.",
                "During a shared task, {$nameA} and {$nameB} bickered constantly, their frustration with each other reaching new heights.",
                "The tribe watched uncomfortably as {$nameA} and {$nameB} exchanged cutting remarks, their animosity plain for all to see.",
                "{$nameA} and {$nameB} discovered they had fundamentally opposing views on an important matter, and neither was willing to compromise.",
                "An unfortunate incident left both {$nameA} and {$nameB} blaming each other, their already strained relationship deteriorating further.",
                "{$nameA} and {$nameB} spent an awkward afternoon together, each irritated by the other's presence and manner of speaking.",
                "What should have been a pleasant collaboration turned sour as {$nameA} and {$nameB} butted heads at every turn, their mutual dislike intensifying."
            ];

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

        // Save the kobolds with updated dataStore
        $koboldA->save();
        $koboldB->save();
    }
}
