<?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('Podcast ID required', 400);
}

try {
    $stmt = $pdo->prepare("
        SELECT p.id, p.title, p.description, p.thumbnail, p.created_at,
               (SELECT COUNT(*) FROM media WHERE podcast_id = p.id) as episode_count
        FROM podcasts p
        WHERE p.id = :id
    ");
    $stmt->execute([':id' => $id]);
    $podcast = $stmt->fetch();
    
    if (!$podcast) {
        sendError('Podcast not found', 404);
    }
    
    define('R2_PUBLIC_URL', 'https://pub-4a26074f63ae4203a6de95f3b950b21c.r2.dev');
    if ($podcast['thumbnail']) {
        $podcast['thumbnail_url'] = R2_PUBLIC_URL . '/' . $podcast['thumbnail'];
    }
    
    sendSuccess($podcast);
    
} catch (PDOException $e) {
    sendError('Database error: ' . $e->getMessage(), 500);
}
?>