<?php
declare(strict_types=1);

require_once __DIR__ . '/../../security/auth_guard.php';
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../core/Response.php';
require_once __DIR__ . '/../../security/auth_guard_web.php';
if (empty($_POST['product_id']) || empty($_FILES['video'])) {
    Response::error('product_id and video are required', 422);
}

$productId = (int)$_POST['product_id'];
$file = $_FILES['video'];

$allowedTypes = ['video/mp4', 'video/webm'];

if (!in_array($file['type'], $allowedTypes, true)) {
    Response::error('Only MP4 or WebM videos allowed', 422);
}

if ($file['size'] > 50 * 1024 * 1024) {
    Response::error('Video exceeds 50MB limit', 422);
}

$uploadDir = __DIR__ . '/../../uploads/retail-products/' . $productId . '/sections/videos/';

if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0775, true);
}

$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$fileName = uniqid('section_video_', true) . '.' . $ext;

if (!move_uploaded_file($file['tmp_name'], $uploadDir . $fileName)) {
    Response::error('Failed to upload video', 500);
}

Response::success('Video uploaded', [
    'file_path' => '/uploads/retail-products/' . $productId . '/sections/videos/' . $fileName
]);
