<?php
require_once __DIR__ . '/../../config/session.php';
require_once __DIR__ . '/../../config/db.php';

if (session_status() === PHP_SESSION_NONE) session_start();
header('Content-Type: application/json; charset=utf-8');

if (empty($_SESSION['user_id'])) {
    echo json_encode(['success'=>false,'message'=>'Unauthorized','tracks'=>[]]);
    exit;
}

try {
    $stmt = $pdo->query("SELECT id,title_bn,track_key,file_path FROM madhab_audio_tracks WHERE is_active=1 ORDER BY id ASC");
    echo json_encode(['success'=>true,'tracks'=>$stmt->fetchAll(PDO::FETCH_ASSOC)]);
} catch (Throwable $e) {
    echo json_encode(['success'=>false,'message'=>$e->getMessage(),'tracks'=>[]]);
}