<?php
require_once __DIR__ . '/config/database.php';
$db = Database::getInstance();
$course = $db->get('SELECT id, title, thumbnail FROM courses WHERE id = ?', [1]);
$thumb = $course['thumbnail'] ?? '';
$baseUrl = defined('APP_URL') ? rtrim(APP_URL, '/') : '';
$thumbCandidate = $thumb ? ltrim($thumb, '/') : '';

if ($thumb && stripos($thumb, 'http') === 0) {
    $thumbSrc = $thumb;
} elseif ($thumbCandidate && file_exists(__DIR__ . '/' . $thumbCandidate)) {
    $thumbSrc = $baseUrl . '/' . $thumbCandidate;
} elseif ($thumbCandidate && file_exists(__DIR__ . '/uploads/courses/' . basename($thumbCandidate))) {
    $thumbSrc = $baseUrl . '/uploads/courses/' . basename($thumbCandidate);
} else {
    $thumbSrc = $baseUrl . '/assets/images/course-placeholder.jpg';
}
header('Content-Type: application/json');
echo json_encode(['thumb' => $thumb, 'thumb_candidate' => $thumbCandidate, 'thumbSrc' => $thumbSrc], JSON_PRETTY_PRINT);
