<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repositories\Interfaces\VideoGalleryInterface;
use Illuminate\Http\Request;

class VideoGalleryController extends Controller
{
    protected $videoRepo;

    public function __construct(VideoGalleryInterface $videoRepo)
    {
        $this->videoRepo = $videoRepo;
    }

    public function index(Request $request)
    {
        $videos = $this->videoRepo->list($request);
        return view('Admin.VideoGallery.list', compact('videos'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'video_id' => 'required|string|max:255|regex:/^[a-zA-Z0-9_-]{11}$/',
            'status'   => 'nullable|in:0,1',
        ]);

        $data = [
            'video_id' => $request->video_id,
            'status'   => $request->status ?? 1,
        ];

        $video = $this->videoRepo->store($data);
        return response()->json(['success' => true, 'video' => $video]);
    }

    public function edit($id)
    {
        $video = $this->videoRepo->find($id);
        if (!$video) {
            return response()->json(['success' => false, 'message' => 'Video not found'], 404);
        }
        return response()->json(['success' => true, 'video' => $video]);
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'video_id' => 'required|string|max:255|regex:/^[a-zA-Z0-9_-]{11}$/',
            'status'   => 'nullable|in:0,1',
        ]);

        $data = [
            'video_id' => $request->video_id,
            'status'   => $request->status ?? 1,
        ];

        $video = $this->videoRepo->update($id, $data);
        if (!$video) {
            return response()->json(['success' => false, 'message' => 'Video not found'], 404);
        }
        return response()->json(['success' => true, 'video' => $video]);
    }

    public function destroy($id)
    {
        $result = $this->videoRepo->delete($id);
        if ($result) {
            return response()->json(['success' => true]);
        }
        return response()->json(['success' => false, 'message' => 'Video not found'], 404);
    }
}
