<?php

namespace App\Http\Controllers\Api;

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

class VideoController extends Controller
{
    public function index()
    {
        return Video::where('is_active', true)
            ->orderBy('sort_order')
            ->orderBy('created_at')
            ->get();
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'title'       => 'required|string|max:255',
            'subtitle'    => 'nullable|string|max:255',
            'youtube_url' => 'required|string|max:500',
            'duration'    => 'nullable|string|max:20',
            'category'    => 'nullable|string|max:100',
            'is_featured' => 'boolean',
            'is_active'   => 'boolean',
            'sort_order'  => 'integer',
        ]);

        return Video::create($data);
    }

    public function show(Video $video)
    {
        return $video;
    }

    public function update(Request $request, Video $video)
    {
        $data = $request->validate([
            'title'       => 'sometimes|required|string|max:255',
            'subtitle'    => 'nullable|string|max:255',
            'youtube_url' => 'sometimes|required|string|max:500',
            'duration'    => 'nullable|string|max:20',
            'category'    => 'nullable|string|max:100',
            'is_featured' => 'boolean',
            'is_active'   => 'boolean',
            'sort_order'  => 'integer',
        ]);

        $video->update($data);
        return $video;
    }

    public function destroy(Video $video)
    {
        $video->delete();
        return response()->noContent();
    }
}
