<?php
// api/public/media-item.php

require_once '../../config/cors.php';
require_once '../../config/db.php';
require_once '../../helpers/response.php';

$id = isset($_GET['id']) ? intval($_GET['id']) : null;

if (!$id) {
    sendError('Media ID required', 400);
}

try {
    $stmt = $pdo->prepare("
        SELECT m.id, m.title, m.description, m.type, m.media_id, m.podcast_id,
               m.is_premium, m.file_size, m.created_at,
               p.title as podcast_title
        FROM media m
        LEFT JOIN podcasts p ON p.id = m.podcast_id
        WHERE m.id = :id AND m.status = 'ready'
    ");
    $stmt->execute([':id' => $id]);
    $media = $stmt->fetch();
    
    if (!$media) {
        sendError('Media not found', 404);
    }
    
    define('R2_PUBLIC_URL', 'https://pub-4a26074f63ae4203a6de95f3b950b21c.r2.dev');
    $media['url'] = R2_PUBLIC_URL . '/' . $media['media_id'];
    
    sendSuccess($media);
    
} catch (PDOException $e) {
    sendError('Database error: ' . $e->getMessage(), 500);
}
?>