<?php
declare(strict_types=1);

require_once __DIR__.'/../../security/auth_guard.php';
require_once __DIR__.'/../../config/constants.php';
require_once __DIR__.'/../../core/Response.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    Response::error('Invalid request method', 405);
}

if (empty($_FILES['video']['tmp_name'])) {
    Response::error('Video is required', 422);
}

$uploadDir = BASE_PATH.'/uploads/hospitality/sections/';
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0775, true);
}

$videoName = 'vid_'.uniqid().'.mp4';
move_uploaded_file($_FILES['video']['tmp_name'], $uploadDir.$videoName);

$thumb = null;
if (!empty($_FILES['thumbnail']['tmp_name'])) {
    $thumbName = 'thumb_'.uniqid().'.webp';
    move_uploaded_file($_FILES['thumbnail']['tmp_name'], $uploadDir.$thumbName);
    $thumb = BASE_URL.UPLOAD_BASE_URL.'/hospitality/sections/'.$thumbName;
}

Response::success('Video uploaded', [
    'file_path' => BASE_URL.UPLOAD_BASE_URL.'/hospitality/sections/'.$videoName,
    'thumbnail_path' => $thumb
]);
