<?php

declare(strict_types=1);

namespace App\Http\Api\Schema\Wiki\Anime\Theme;

use App\Contracts\Http\Api\Schema\SearchableSchema;
use App\Http\Api\Field\Base\IdField;
use App\Http\Api\Field\Field;
use App\Http\Api\Field\Wiki\Anime\Theme\Entry\EntryEpisodesField;
use App\Http\Api\Field\Wiki\Anime\Theme\Entry\EntryNotesField;
use App\Http\Api\Field\Wiki\Anime\Theme\Entry\EntryNsfwField;
use App\Http\Api\Field\Wiki\Anime\Theme\Entry\EntrySpoilerField;
use App\Http\Api\Field\Wiki\Anime\Theme\Entry\EntryThemeIdField;
use App\Http\Api\Field\Wiki\Anime\Theme\Entry\EntryTrackCountField;
use App\Http\Api\Field\Wiki\Anime\Theme\Entry\EntryVersionField;
use App\Http\Api\Include\AllowedInclude;
use App\Http\Api\Schema\EloquentSchema;
use App\Http\Api\Schema\Wiki\Anime\ThemeSchema;
use App\Http\Api\Schema\Wiki\AnimeSchema;
use App\Http\Api\Schema\Wiki\ArtistSchema;
use App\Http\Api\Schema\Wiki\AudioSchema;
use App\Http\Api\Schema\Wiki\GroupSchema;
use App\Http\Api\Schema\Wiki\ImageSchema;
use App\Http\Api\Schema\Wiki\VideoSchema;
use App\Http\Resources\Wiki\Anime\Theme\Resource\EntryJsonResource;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;

class EntrySchema extends EloquentSchema implements SearchableSchema
{
    public function type(): string
    {
        return EntryJsonResource::$wrap;
    }

    /**
     * @return AllowedInclude[]
     */
    public function allowedIncludes(): array
    {
        return $this->withIntermediatePaths([
            new AllowedInclude(new AnimeSchema(), AnimeThemeEntry::RELATION_ANIME),
            new AllowedInclude(new ThemeSchema(), AnimeThemeEntry::RELATION_THEME),
            new AllowedInclude(new VideoSchema(), AnimeThemeEntry::RELATION_VIDEOS),

            // Undocumented paths needed for client builds
            new AllowedInclude(new ImageSchema(), 'animetheme.anime.images'),
            new AllowedInclude(new ArtistSchema(), 'animetheme.song.artists'),
            new AllowedInclude(new GroupSchema(), AnimeThemeEntry::RELATION_THEME_GROUP),
            new AllowedInclude(new AudioSchema(), 'videos.audio'),
        ]);
    }

    /**
     * @return Field[]
     */
    public function fields(): array
    {
        return array_merge(
            parent::fields(),
            [
                new IdField($this, AnimeThemeEntry::ATTRIBUTE_ID),
                new EntryEpisodesField($this),
                new EntryNotesField($this),
                new EntryNsfwField($this),
                new EntrySpoilerField($this),
                new EntryVersionField($this),
                new EntryTrackCountField($this),
                new EntryThemeIdField($this),
            ],
        );
    }

    /**
     * Get the model of the schema.
     */
    public function model(): AnimeThemeEntry
    {
        return new AnimeThemeEntry();
    }
}
