<?php
// Get coaches from database
require_once __DIR__ . '/../../api/lib.php';
$coaches = [];
try {
    $pdo = hw_db();
    $stmt = $pdo->query("SELECT c.*, u.first_name, u.last_name FROM coaches c JOIN users u ON c.user_id = u.id ORDER BY c.id");
    $coaches = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    // Fallback to static data if DB fails
}
?>
<div class="w-full max-w-md">
    <div id="default-msg" class="text-center p-12 bg-amber-50/50 border-2 border-dashed border-amber-200 rounded-[2rem]">
        <div class="mb-4 flex justify-center">
            <svg class="w-16 h-16 text-amber-600/20" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 21l-8.228-9.904A17.963 17.963 0 0112 2.244c4.171 0 8.079 1.422 11.228 3.852L12 21z"></path></svg>
        </div>
        <p class="serif text-xl italic text-gray-500">Discover a branch of healing wisdom.</p>
        <p class="text-xs text-gray-400 mt-2 uppercase tracking-widest">Select an active point on the tree</p>
    </div>

    <?php foreach ($coaches as $index => $coach): 
        $coachId = $index + 1;
        $expertise = json_decode($coach['expertise'] ?? '[]', true);
        $primaryExpertise = $expertise[0] ?? 'Wellness';
        $isAncestral = strpos($primaryExpertise, 'Ancestral') !== false;
        $badgeColor = $isAncestral ? 'amber' : ($primaryExpertise === 'Mind-Body Balance' ? 'blue' : 'green');
        $buttonColor = $isAncestral ? 'amber-600' : 'shining-green';
    ?>
    <div id="coach-<?= $coachId ?>" class="coach-info-card bg-white p-8 rounded-[2.5rem] shadow-2xl shadow-green-900/10 border border-green-50" style="display:none">
        <div class="flex items-center gap-6 mb-8">
            <img src="https://i.pravatar.cc/150?u=<?= strtolower($coach['first_name']) ?>" class="w-24 h-24 rounded-2xl object-cover shadow-lg">
            <div>
                <h4 class="serif text-3xl font-bold text-gray-900"><?= htmlspecialchars($coach['display_name']) ?></h4>
                <span class="bg-<?= $badgeColor ?>-100 text-<?= $badgeColor ?>-800 px-3 py-1 rounded-full text-[10px] font-bold uppercase tracking-widest"><?= htmlspecialchars($primaryExpertise) ?></span>
            </div>
        </div>
        <p class="text-gray-600 mb-8 leading-relaxed italic text-lg">"<?= htmlspecialchars($coach['bio']) ?>"</p>
        <button onclick="bookCoach(<?= $coach['id'] ?>, '<?= htmlspecialchars($coach['display_name']) ?>')" class="w-full block py-4 bg-<?= $buttonColor ?> text-white rounded-2xl font-bold shadow-lg hover:opacity-90 transition-opacity">
            <?= $isAncestral ? ($coach['display_name'] === 'Mama Nia' ? 'Ancestral Consultation' : 'Lineage Reading') : ($primaryExpertise === 'Mind-Body Balance' ? 'Schedule Virtual Session' : ($coach['display_name'] === 'Dr. Amara Okoro' ? 'Nutritional Strategy' : 'Secure Clinical Consult')) ?>
        </button>
    </div>
    <?php endforeach; ?>
</div>

<script>
function bookCoach(coachId, coachName) {
    // Check if user is logged in
    fetch('/files/atasteofafricanmarketplace/api/auth/me.php')
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                // User is logged in, proceed to booking
                window.location.href = `/files/atasteofafricanmarketplace/book.php?coach=${coachId}`;
            } else {
                // Redirect to login with return URL
                window.location.href = `/files/atasteofafricanmarketplace/login.php?return=${encodeURIComponent('/files/atasteofafricanmarketplace/book.php?coach=' + coachId)}`;
            }
        })
        .catch(() => {
            // On error, redirect to login
            window.location.href = `/files/atasteofafricanmarketplace/login.php?return=${encodeURIComponent('/files/atasteofafricanmarketplace/book.php?coach=' + coachId)}`;
        });
}
</script>
