<?php
namespace App\Http\Controllers\Admin;

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

class VideoController extends Controller
{
    // Show all videos
    public function index()
    {
        $videos = Video::latest()->paginate(10);
        return view('admin.videos.index', compact('videos'));
    }

    // Show form to create video
    public function create()
    {
        return view('admin.videos.create');
    }

    // Store new video
    public function store(Request $request)
    {
        $request->validate([
            'title'     => 'required|string|max:255',
            'thumbnail' => 'required|image|mimes:jpg,jpeg,png,gif|max:2048',
            'url'       => 'required|url',
            'platform'  => 'nullable|string',
            'status'    => 'required|in:active,inactive',
        ]);

        // Upload thumbnail
        if ($request->hasFile('thumbnail')) {
            $file      = $request->file('thumbnail');
            $filename  = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('uploads/videos'), $filename);
            $thumbnailPath = 'uploads/videos/' . $filename;
        }

        Video::create([
            'title'     => $request->title,
            'thumbnail' => $thumbnailPath ?? null,
            'url'       => $request->url,
            'platform'  => $request->platform,
            'status'    => $request->status,
        ]);

        return redirect()->route('admin.videos.index')->with('success', 'Video added successfully!');
    }

    // Edit video
    public function edit($id)
    {
        $video = Video::findOrFail($id);
        return view('admin.videos.edit', compact('video'));
    }

    // Update video
    public function update(Request $request, $id)
    {
        $video = Video::findOrFail($id);

        $request->validate([
            'title'     => 'required|string|max:255',
            'thumbnail' => 'nullable|image|mimes:jpg,jpeg,png,gif|max:2048',
            'url'       => 'required|url',
            'platform'  => 'nullable|string',
            'status'    => 'required|in:active,inactive',
        ]);

        if ($request->hasFile('thumbnail')) {
            $file      = $request->file('thumbnail');
            $filename  = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('uploads/videos'), $filename);
            $thumbnailPath = 'uploads/videos/' . $filename;
        } else {
            $thumbnailPath = $video->thumbnail;
        }

        $video->update([
            'title'     => $request->title,
            'thumbnail' => $thumbnailPath,
            'url'       => $request->url,
            'platform'  => $request->platform,
            'status'    => $request->status,
        ]);

        return redirect()->route('admin.videos.index')->with('success', 'Video updated successfully!');
    }

    // Delete video
    public function destroy($id)
    {
        $video = Video::findOrFail($id);

        if ($video->thumbnail && file_exists(public_path($video->thumbnail))) {
            unlink(public_path($video->thumbnail));
        }

        $video->delete();

        return redirect()->route('admin.videos.index')->with('success', 'Video deleted successfully!');
    }
}
