<?php

namespace App\Http\Controllers\Admin;

use App\Enums\UserRole;
use App\Http\Controllers\Controller;
use App\Jobs\DiscordReleaseNotification;
use App\Models\Episode;
use App\Services\DownloadService;
use App\Services\EpisodeService;
use App\Services\GalleryService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class EpisodeController extends Controller
{
    protected EpisodeService $episodeService;

    protected GalleryService $galleryService;

    protected DownloadService $downloadService;

    public function __construct(
        EpisodeService $episodeService,
        GalleryService $galleryService,
        DownloadService $downloadService
    ) {
        $this->episodeService = $episodeService;
        $this->galleryService = $galleryService;
        $this->downloadService = $downloadService;
    }

    /**
     * Add Episode to existing series
     */
    public function store(Request $request): RedirectResponse
    {
        $referenceEpisode = Episode::with('hentai')->where('id', $request->input('episode_id'))->firstOrFail();
        $episodeNumber = $referenceEpisode->hentai->episodes()->count() + 1;

        // Create Episode
        $episode = $this->episodeService->createEpisode($request, $referenceEpisode->hentai, $episodeNumber, null, $referenceEpisode);
        $this->episodeService->createOrUpdateCover($request, $episode, $referenceEpisode->hentai->slug, 1);
        $this->downloadService->createOrUpdateDownloads($request, $episode, 1);
        $this->galleryService->createOrUpdateGallery($request, $referenceEpisode->hentai, $episode, $episodeNumber, true);

        // Discord Alert
        if ($request->has('censored')) {
            DiscordReleaseNotification::dispatch($referenceEpisode->title.' - '.$episodeNumber, 'release-censored');
        } else {
            DiscordReleaseNotification::dispatch($episode->slug, 'release');
        }

        cache()->flush();

        return to_route('hentai.index', [
            'title' => $episode->slug,
        ]);
    }

    /**
     * Edit Episode
     */
    public function update(Request $request): RedirectResponse
    {
        $episode = Episode::with('hentai')->where('id', $request->input('episode_id'))->firstOrFail();

        if ($request->user()->hasRole(UserRole::MODERATOR)) {
            $this->episodeService->updateEpisodeModerator($request, $episode->id);

            cache()->flush();

            return to_route('hentai.index', [
                'title' => $episode->slug,
            ]);
        }

        $studio = $this->episodeService->getOrCreateStudio(json_decode($request->input('studio'))[0]->value);

        $oldinterpolated = $episode->interpolated;
        $oldInterpolatedUHD = $episode->interpolated_uhd;

        $episode = $this->episodeService->updateEpisode($request, $studio, $episode->id);
        $this->episodeService->createOrUpdateCover($request, $episode, $episode->hentai->slug, 1);
        $this->downloadService->createOrUpdateDownloads($request, $episode, 1);
        $this->galleryService->createOrUpdateGallery($request, $episode->hentai, $episode, $episode->episode, true);

        // Discord Alert
        if ($oldinterpolated !== (int) $episode->interpolated) {
            DiscordReleaseNotification::dispatch($episode->slug, 'update');
        }

        if ($oldInterpolatedUHD !== (int) $episode->interpolated_uhd) {
            DiscordReleaseNotification::dispatch($episode->slug, 'updateUHD');
        }

        if ($request->has('v2')) {
            DiscordReleaseNotification::dispatch($episode->slug, 'v2');
        }

        cache()->flush();

        return to_route('hentai.index', [
            'title' => $episode->slug,
        ]);
    }
}
