<?php
/**
 * Heritage Voice — offline.php
 * Handlers cho offline_packs endpoints
 */

function offline_pack(string $tour_id): void {
    $lang = str_param('lang', 'vi');
    $stmt = db()->prepare("
        SELECT op.*, vt.title AS tour_title, hs.name AS site_name
        FROM offline_packs op
        JOIN voice_tours    vt ON vt.id = op.tour_id
        JOIN heritage_sites hs ON hs.id = op.site_id
        WHERE op.tour_id  = :tour_id
          AND op.language = :lang
          AND op.status   = 'active'
        ORDER BY op.created_at DESC
        LIMIT 1
    ");
    $stmt->execute([':tour_id' => $tour_id, ':lang' => $lang]);
    $row = $stmt->fetch();
    if (!$row) json_error('Không tìm thấy gói offline', 404);

    $row['package_size']       = (int)$row['package_size'];
    $row['include_audio']      = (bool)$row['include_audio'];
    $row['include_transcript'] = (bool)$row['include_transcript'];
    $row['include_images']     = (bool)$row['include_images'];
    $row['include_map']        = (bool)$row['include_map'];
    json_ok($row);
}
