<?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['image']['tmp_name'])) {
    Response::error('Image is required', 422);
}

$file = $_FILES['image'];

if ($file['size'] > MAX_IMAGE_SIZE) {
    Response::error('Image too large');
}

$mime = mime_content_type($file['tmp_name']);
if (!in_array($mime, ALLOWED_IMAGE_TYPES, true)) {
    Response::error('Invalid image type');
}

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

$name = 'img_'.uniqid().'.webp';
move_uploaded_file($file['tmp_name'], $uploadDir.$name);

Response::success('Image uploaded', [
    'file_path' => BASE_URL.UPLOAD_BASE_URL.'/hospitality/sections/'.$name
]);
