<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Episode;
use App\Models\Season;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use function redirect;
use function request;
use function view;

class EpisodeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Application|Factory|View
     */
    public function index()
    {
        $episodes = Episode::paginate(5);
        return view('admin.episodes.index', compact('episodes'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Application|Factory|View
     */
    public function create()
    {
        $seasons = Season::orderby('number','asc')->get();
        return view('admin.episodes.create', compact('seasons'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return RedirectResponse
     */
    public function store(Request $request): RedirectResponse
    {
        request()->validate([
            'title' => 'required',
            'description' => 'required',
            'season' => 'required',
            'link' => 'required',
            'status' => 'required',
            'home' => 'required'
        ]);

        if ($request->home == 1){
            DB::table('episodes')->update(array('home' => 0));
        }
        Episode::create($request->all());
        return redirect()->route('episodes.index');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return void
     */
    public function show(int $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Episode $episode
     * @return Application|Factory|View
     */
    public function edit(Episode $episode)
    {
        $seasons = Season::orderby('number', 'asc')->get();
        return view('admin.episodes.edit', compact('episode','seasons'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Episode $episode
     * @return RedirectResponse
     */
    public function update(Request $request, Episode $episode): RedirectResponse
    {
        request()->validate([
            'title' => 'required',
            'description' => 'required',
            'season' => 'required',
            'link' => 'required',
            'status' => 'required',
            'home' => 'required'
        ]);
        if ($request->home == 1){
            DB::table('episodes')->update(array('home' => 0));
        }
        $episode->update($request->all());
        return redirect()->route('episodes.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Episode $episode
     * @return RedirectResponse
     */
    public function destroy(Episode $episode)
    {
        $episode->delete();
        return redirect()->route('episodes.index');
    }
}
