<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Video;

class VideoController extends Controller
{
    public function index()
    {
        return view('admin.video.index', [
            'videos' => Video::orderBy('id', 'desc')->get()
        ]);
    }

    public function videoLainnya()
    {
        $videos = Video::latest()->get(); // atau paginate(8)
        return view('video.video', compact('videos'));
    }

    public function store(Request $request)
    {
        $rules = [
            'judul' => 'required',
            'youtube_code' => 'required',
        ];

        $message = [
            'judul.required' => 'Judul Wajib diisi',
            'youtube_code.required' => 'Youtube Code Wajib diisi',
        ];

        $this->validate($request, $rules, $message);

        Video::create([
            'judul' => $request->judul,
            'youtube_code' => $request->youtube_code,
        ]);

        return redirect(route('video'))
            ->with('success', 'data Video berhasil di simpan');
    }

    # Fungsi Update
    public function update(Request $request, $id)
    {
        $video = Video::find($id);

        $rules = [
            'judul' => 'required',
            'youtube_code' => 'required',
        ];

        $messages = [
            'judul.required' => 'Judul wajib diisi',
            'youtube_code.required' => 'Code Youtube wajib diisi',
        ];

        $this->validate($request, $rules, $messages);

        $video->update([
            'judul' => $request->judul,
            'youtube_code' => $request->youtube_code,
        ]);

        return redirect(route('video'))
            ->with('success', 'data video berhasil diUpdate');
    }

    public function destroy($id)
    {
        $video = Video::find($id);

        // Hapus data dari database
        $video->delete();

        return redirect(route('video'))
            ->with('success', 'Video berhasil dihapus');
    }
}
