<?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__.'/../../core/Validator.php';

$db = Database::getInstance();
$data = json_decode(file_get_contents('php://input'), true);

Validator::required($data, [
    'hospitality_product_id',
    'file_type',
    'file_path',
    'priority'
]);

$stmt = $db->prepare("
INSERT INTO hospitality_product_sections (
    hospitality_product_id,
    section_title,
    section_key,
    short_description,
    description,
    file_type,
    preview_type,
    file_path,
    thumbnail_path,
    alt_text,
    caption,
    priority,
    status,
    approval_status,
    created_by,
    created_at
) VALUES (
    :pid,:title,:key,:short,:desc,:type,:preview,
    :file,:thumb,:alt,:caption,:priority,
    'Draft','Pending',:by,NOW()
)");

$stmt->execute([
    'pid'     => $data['hospitality_product_id'],
    'title'   => $data['section_title'] ?? null,
    'key'     => $data['section_key'] ?? null,
    'short'   => $data['short_description'] ?? null,
    'desc'    => $data['description'] ?? null,
    'type'    => $data['file_type'],
    'preview' => $data['preview_type'] ?? 'Both',
    'file'    => $data['file_path'],
    'thumb'   => $data['thumbnail_path'] ?? null,
    'alt'     => $data['alt_text'] ?? null,
    'caption' => $data['caption'] ?? null,
    'priority'=> (int)$data['priority'],
    'by'      => $_SESSION['employee_id']
]);

Response::success('Section created');
