<?php
$moodLabels    = [1 => '😞 يوم صعب', 2 => '😕 يوم غير مريح', 3 => '😐 يوم عادي', 4 => '😊 يوم جيد', 5 => '🌟 يوم رائع'];
$moodColors    = [1 => 'danger', 2 => 'warning', 3 => 'secondary', 4 => 'info', 5 => 'success'];
$moodIcons     = [1 => 'fa-face-sad-tear text-danger', 2 => 'fa-face-frown text-warning', 3 => 'fa-face-meh text-secondary', 4 => 'fa-face-smile text-info', 5 => 'fa-face-laugh-beam text-success'];

$energyLabels  = [1 => '🔋 منهكة', 2 => '🔋 منخفضة', 3 => '⚡ متوسطة', 4 => '⚡ جيدة', 5 => '⚡⚡ عالية'];
?>

<div class="container-fluid py-4 animate-fade-in">
    <!-- Header -->
    <div class="row mb-5 align-items-center">
        <div class="col-md-8">
            <h1 class="fw-bold text-dark display-6 mb-2"><i class="fa-solid fa-timeline text-primary me-2"></i> العرض الزمني لليوميات</h1>
            <p class="text-secondary fs-5 mb-0">استعرض تقدمك وحالتك المزاجية عبر الأيام الماضية (آخر 30 يوماً).</p>
        </div>
        <div class="col-md-4 text-md-end mt-3 mt-md-0">
            <a href="<?= BASE_URL ?>/journal" class="btn btn-outline-secondary btn-premium rounded-pill px-4"><i class="fa-solid fa-arrow-left me-2"></i> عودة لليومية</a>
        </div>
    </div>

    <?php if (empty($entries)): ?>
        <div class="card card-saas border-0 text-center py-5">
            <div class="card-body">
                <i class="fa-solid fa-book-open fa-4x text-muted opacity-25 mb-4"></i>
                <h3 class="fw-bold text-muted mb-3">لا توجد يوميات مسجلة بعد</h3>
                <p class="text-secondary">ابدأ بكتابة يومياتك الأولى من مركز القيادة لبناء أرشيفك الشخصي.</p>
                <a href="<?= BASE_URL ?>/journal" class="btn btn-primary btn-premium rounded-pill px-4 mt-3">اكتب يومياتك الآن</a>
            </div>
        </div>
    <?php else: ?>
        <!-- Timeline Container -->
        <div class="position-relative py-3" style="max-width: 900px; margin: 0 auto;">
            <!-- Vertical Line -->
            <div class="position-absolute" style="top: 0; bottom: 0; right: 28px; width: 4px; background-color: var(--bs-primary); opacity: 0.15; border-radius: 4px;"></div>
            
            <?php foreach ($entries as $index => $entry): 
                $moodVal = max(1, min(5, (int)$entry->mood));
                $energyVal = max(1, min(5, (int)$entry->energy_level));
                $mColor = $moodColors[$moodVal];
                $delay = min(5, $index + 1);
            ?>
            <div class="position-relative mb-5 animate-slide-up delay-<?= $delay ?>">
                <!-- Timeline Dot/Icon -->
                <div class="position-absolute bg-white rounded-circle shadow d-flex align-items-center justify-content-center" 
                     style="width: 60px; height: 60px; right: 0; top: 0; border: 4px solid var(--bs-<?= $mColor ?>); z-index: 2;">
                    <i class="fa-solid <?= $moodIcons[$moodVal] ?> fs-3"></i>
                </div>

                <!-- Timeline Content Card -->
                <div class="card card-saas border-0 shadow-sm" style="margin-right: 85px;">
                    <div class="card-header bg-white border-bottom-0 pt-4 pb-0 px-4 d-flex justify-content-between align-items-center flex-wrap">
                        <div>
                            <h4 class="fw-bold text-dark mb-1"><?= date('l, d M Y', strtotime($entry->date)) ?></h4>
                            <span class="text-muted small fw-semibold"><i class="fa-regular fa-clock me-1"></i> سُجلت في: <?= date('h:i A', strtotime($entry->created_at)) ?></span>
                        </div>
                        <div class="d-flex gap-2 mt-2 mt-sm-0">
                            <span class="badge bg-<?= $mColor ?>-subtle text-<?= $mColor ?> rounded-pill px-3 py-2 fw-bold fs-6">
                                <?= $moodLabels[$moodVal] ?>
                            </span>
                            <span class="badge bg-light text-dark border rounded-pill px-3 py-2 fw-bold fs-6">
                                <?= $energyLabels[$energyVal] ?>
                            </span>
                        </div>
                    </div>

                    <div class="card-body p-4">
                        <?php if (!empty(trim($entry->achieved))): ?>
                            <div class="mb-4">
                                <h6 class="fw-bold text-success mb-2"><i class="fa-solid fa-trophy me-2"></i> ما تم إنجازه:</h6>
                                <div class="bg-success-subtle bg-opacity-10 p-3 rounded-3 border-start border-4 border-success text-dark fs-6" style="line-height: 1.6;">
                                    <?= nl2br(htmlspecialchars($entry->achieved)) ?>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if (!empty(trim($entry->failed))): ?>
                            <div class="mb-4">
                                <h6 class="fw-bold text-danger mb-2"><i class="fa-solid fa-circle-xmark me-2"></i> إخفاقات وتحديات:</h6>
                                <div class="bg-danger-subtle bg-opacity-10 p-3 rounded-3 border-start border-4 border-danger text-dark fs-6" style="line-height: 1.6;">
                                    <?= nl2br(htmlspecialchars($entry->failed)) ?>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if (!empty(trim($entry->notes))): ?>
                            <div>
                                <h6 class="fw-bold text-secondary mb-2"><i class="fa-solid fa-note-sticky me-2"></i> ملاحظات و أفكار:</h6>
                                <div class="bg-light p-3 rounded-3 border-start border-4 border-secondary text-dark fs-6" style="line-height: 1.6;">
                                    <?= nl2br(htmlspecialchars($entry->notes)) ?>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
