<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\VideoItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;

class VideoItemController extends Controller
{
    // List all images
    public function index()
    {
        $videos = VideoItem::all();
        return response()->json($videos, 200);
    }

    // Store a new image
    public function store(Request $request)
    {
        Log::error($request);

        $validator = Validator::make($request->all(), [
            'title' => 'required|string|max:255',
            'description' => 'required|string|max:255',
            'video' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048', // Adjust as needed
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

        // Handle file upload
        if ($request->hasFile('video')) {
            $path = $request->file('video')->store('videos', 'public');
        }

        $video = VideoItem::create([
            'title' => $request->title,
            'description' => $request->description,
            'video_path' => $path,
        ]);

        return response()->json($video, 201);
    }

    // Show a single image
    public function show($id)
    {
        $video = VideoItem::find($id);

        if (!$video) {
            return response()->json(['message' => 'video not found'], 404);
        }

        return response()->json($video, 200);
    }

    // Update an image
    public function update(Request $request, $id)
    {
        $video = VideoItem::find($id);

        if (!$video) {
            return response()->json(['message' => 'Video not found'], 404);
        }

        $validator = Validator::make($request->all(), [
            'title' => 'sometimes|required|string|max:255',
            'description' => 'sometimes|required|string|max:255',
            'video' => 'sometimes|required|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048', // Adjust as needed
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

        // Update title if provided
        if ($request->has('title')) {
            $video->title = $request->title;
        }

        if($request->has('description')) {
            $video->description = $request->description;
        }

        // Handle new image upload
        if ($request->hasFile('video')) {
            // Delete old image
            if (Storage::disk('public')->exists($video->video_path)) {
                Storage::disk('public')->delete($video->video_path);
            }

            // Store new image
            $path = $request->file('video')->store('videos', 'public');
            $video->video_path = $path;
        }

        $video->save();

        return response()->json($video, 200);
    }

    // Delete an image
    public function destroy($id)
    {
        $video = VideoItem::find($id);

        if (!$video) {
            return response()->json(['message' => 'Video not found'], 404);
        }

        // Delete image file
        if (Storage::disk('public')->exists($video->video_path)) {
            Storage::disk('public')->delete($video->video_path);
        }

        $video->delete();

        return response()->json(['message' => 'Video deleted successfully'], 200);
    }
} : Internal Server Error

QueryException
SQLSTATE[HY000]: General error: 1364 Field 'video_url' doesn't have a default value (Connection: mysql, SQL: insert into `video_items` (`title`, `description`, `updated_at`, `created_at`) values (likan, lorem lorem, 2024-10-22 19:05:34, 2024-10-22 19:05:34)) ; [2024-10-22 19:02:26] local.ERROR: array (
  'title' => 'likan',
  'description' => 'lorem lorem',
  'video' => 
  \Illuminate\Http\UploadedFile::__set_state(array(
     'test' => false,
     'originalName' => 'Rectangle 883.png',
     'mimeType' => 'image/png',
     'error' => 0,
     'originalPath' => 'Rectangle 883.png',
     'hashName' => NULL,
  )),
)