<?php

namespace App\Http\Controllers;

use App\Models\Episode;
use App\Helpers\CacheHelper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;

class HomeController extends Controller
{
    /**
     * Display Home Page.
     */
    public function index(): \Illuminate\View\View
    {
        $guest = Auth::guest();

        $guestString = $guest ? 'guest' : 'authed';

        $mostLikes = \cache()->remember('mostLikes'.$guestString, 300, fn () => 
            Episode::with('gallery')
                ->when($guest, fn ($query) => $query->withoutTags(['loli', 'shota']))
                ->whereIn('id', function($query) {
                    $mostLikesIds = CacheHelper::getMostLikes()->pluck('markable_id')->toArray();
                    $query->selectRaw('id')
                        ->from('episodes')
                        ->whereIn('id', $mostLikesIds)
                        ->orderByRaw("FIELD(id, " . implode(',', $mostLikesIds) . ")");
                })
                ->get()
        );

        return view('home.index', [
            'recentlyReleased'  => CacheHelper::getRecentlyReleased($guest),
            'recentlyUploaded'  => CacheHelper::getRecentlyUploaded($guest),
            'popularAllTime'    => CacheHelper::getPopularAllTime($guest),
            'popularMonthly'    => CacheHelper::getPopularMonthly(),
            'popularWeekly'     => CacheHelper::getPopularWeekly(),
            'popularDaily'      => CacheHelper::getPopularDaily(),
            'mostLikes'         => $mostLikes,
            'latestComments'    => CacheHelper::getLatestComments(),
        ]);
    }

    /**
     * Display Banned Page.
     */
    public function banned(): \Illuminate\View\View
    {
        return view('auth.banned');
    }

    /**
     * Display Search Page.
     */
    public function search(): \Illuminate\View\View
    {
        return view('search.index');
    }

    /**
     * Display Download Search Page.
     */
    public function downloadSearch(): \Illuminate\View\View
    {
        return view('search.download');
    }

    /**
     * Redirect POST Data to GET with Query String.
     */
    public function searchRedirect(Request $request): \Illuminate\Http\RedirectResponse
    {
        return redirect()->route('hentai.search', [
            'search' => $request->input('live-search'),
        ]);
    }

    /**
     * Display Stats Page.
     */
    public function stats(): \Illuminate\View\View
    {
        return view('home.stats', [
            'viewCount'     => CacheHelper::getTotalViewCount(),
            'episodeCount'  => CacheHelper::getTotalEpisodeCount(),
            'hentaiCount'   => CacheHelper::getTotalHentaiCount(),
            'monthlyCount'  => CacheHelper::getTotalMonthlyViews()
        ]);
    }

    /**
     * Manually set website language
     */
    public function updateLanguage(Request $request): \Illuminate\Http\RedirectResponse
    {
        if(! in_array($request->language, config('lang-detector.languages'))) {
            return redirect()->back();
        }

        Cookie::queue(Cookie::forever('locale', $request->language));

        return redirect()->back();
    }
}
