<?php

namespace App\Http\Controllers\Api;

use App\Helpers\CacheHelper;
use App\Models\Episode;
use App\Models\Studios;
use App\Models\Subtitle;
use App\Http\Controllers\Controller;

class AdminApiController extends Controller
{
    /**
     * Get Tags (API).
     */
    public function getTags()
    {
        $tags = CacheHelper::getAllTags();

        $tagWhiteList = [];
        foreach ($tags as $tag) {
            $tagWhiteList[] = $tag->name;
        }

        return response()->json(['message' => 'success', 'tags' => $tagWhiteList], 200);
    }

    /**
     * Get Studios (API).
     */
    public function getStudios()
    {
        $studios = Studios::orderBy('name', 'ASC')->get();

        $studioList = [];
        foreach ($studios as $studio) {
            $studioList[] = $studio->name;
        }

        return response()->json(['message' => 'success', 'studios' => $studioList], 200);
    }

    /**
     * Get Subtitles (API).
     */
    public function getSubtitles(int $episode_id)
    {
        $subs = Subtitle::all();

        $subsWhiteList = [];
        foreach ($subs as $sub) {
            $subsWhiteList[] = $sub->name;
        }

        $episode = Episode::where('id', $episode_id)->firstOrFail();
        $episodetags = [];
        foreach ($episode->subtitles as $tag) {

            $episodetags[] = $tag->subtitle->name;
        }

        return response()->json(['message' => 'success', 'subs' => $subsWhiteList, 'episodesubs' => $episodetags], 200);
    }

    /**
     * Get Episode Tags (API).
     */
    public function getEpisodeTags(int $episode_id)
    {
        $tags = CacheHelper::getAllTags();
        $tagWhiteList = [];
        foreach ($tags as $tag) {
            $tagWhiteList[] = $tag->name;
        }

        $episode = Episode::where('id', $episode_id)->firstOrFail();
        $episodetags = [];
        foreach ($episode->tags as $tag) {
            $episodetags[] = $tag->name;
        }

        return response()->json(['message' => 'success', 'tags' => $tagWhiteList, 'episodetags' => $episodetags], 200);
    }

    /**
     * Get Episode Studio (API).
     */
    public function getEpisodeStudio(int $episode_id)
    {
        $studios = Studios::orderBy('name', 'ASC')->get();

        $studioList = [];
        foreach ($studios as $studio) {
            $studioList[] = $studio->name;
        }

        $episode = Episode::where('id', $episode_id)->firstOrFail();
        $episodestudio = [$episode->studio->name];

        return response()->json(['message' => 'success', 'studios' => $studioList, 'episodestudios' => $episodestudio], 200);
    }
}
