<?php

declare(strict_types=1);

namespace App\Events\Wiki\Anime\Synonym;

use App\Contracts\Events\UpdateRelatedIndicesEvent;
use App\Events\Base\Wiki\WikiRestoredEvent;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Anime\AnimeSynonym;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Series;
use App\Models\Wiki\Video;

/**
 * @extends WikiRestoredEvent<AnimeSynonym>
 */
class SynonymRestored extends WikiRestoredEvent implements UpdateRelatedIndicesEvent
{
    /**
     * The anime that the synonym belongs to.
     */
    protected Anime $anime;

    public function __construct(AnimeSynonym $synonym)
    {
        parent::__construct($synonym);
        $this->anime = $synonym->anime;
    }

    public function getModel(): AnimeSynonym
    {
        return $this->model;
    }

    protected function getDiscordMessageDescription(): string
    {
        return "Synonym '**{$this->getModel()->getName()}**' has been restored for Anime '**{$this->anime->getName()}**'.";
    }

    public function updateRelatedIndices(): void
    {
        $synonym = $this->getModel()->load([
            AnimeSynonym::RELATION_SERIES,
            AnimeSynonym::RELATION_VIDEOS,
        ]);

        $synonym->anime->searchable();
        $synonym->anime->series->each(fn (Series $series) => $series->searchable());
        $synonym->anime->animethemes->each(function (AnimeTheme $theme): void {
            $theme->searchable();
            $theme->animethemeentries->each(function (AnimeThemeEntry $entry): void {
                $entry->searchable();
                $entry->videos->each(fn (Video $video) => $video->searchable());
            });
        });
    }
}
