<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$scheduleFile = __DIR__ . '/data/schedule.txt';
$schedule = [];

/* Load schedule file */
if (file_exists($scheduleFile)) {
    $lines = file($scheduleFile, FILE_IGNORE_NEW_LINES);
    foreach ($lines as $line) {
        $parts = explode('|', trim($line));
        if (count($parts) === 3) {
            [$day, $hour, $dj] = $parts; // hour like "06:00"
            $schedule[$day][$hour] = $dj;
        }
    }
}

/* Convert 24h to 12h format */
function formatHour12($hour24) {
    $hour = (int)substr($hour24, 0, 2);
    $suffix = $hour >= 12 ? 'PM' : 'AM';
    $hour12 = $hour % 12;
    if ($hour12 === 0) $hour12 = 12;
    return $hour12 . ':00 ' . $suffix;
}

/* Ensure 24h is consistently HH:00 */
function formatHour24($hour24) {
    $h = (int)substr($hour24, 0, 2);
    return str_pad((string)$h, 2, '0', STR_PAD_LEFT) . ':00';
}

/* Combined display: 12h (24h) */
function displayHour($hour24) {
    return formatHour12($hour24) . ' <span class="time-24">(' . formatHour24($hour24) . ')</span>';
}

/* Get current EST hour + day */
$nowEST = new DateTime("now", new DateTimeZone("America/New_York"));
$currentHourEST = $nowEST->format("H") . ":00";
$currentDayEST = $nowEST->format("l"); // Monday, Tuesday, etc.
?>

<div class="main-wrap">
    <div class="content-panel">

        <!-- PAGE HEADER -->
        <div class="schedule-banner">
            <h1 class="schedule-banner-text">📅 Weekly DJ Schedule</h1>
            <p class="schedule-subtext">All times are Eastern Time — shown as 12-hour (24-hour)</p>
        </div>

        <div class="vinyl-divider center-divider"></div>

        <!-- SCHEDULE GRID -->
        <?php foreach ($schedule as $day => $slots): ?>

            <?php
            // Split into Morning (00-11) and Afternoon/Evening (12-23)
            $morningSlots = [];
            $aftEveSlots  = [];

            foreach ($slots as $hour => $dj) {
                $h = (int)substr($hour, 0, 2);
                if ($h < 12) {
                    $morningSlots[$hour] = $dj;
                } else {
                    $aftEveSlots[$hour] = $dj;
                }
            }

            // Sort by hour ascending
            $sortByHour = function($a, $b) {
                return ((int)substr($a, 0, 2)) <=> ((int)substr($b, 0, 2));
            };
            uksort($morningSlots, $sortByHour);
            uksort($aftEveSlots, $sortByHour);
            ?>

            <div class="neon-card schedule-day">

                <!-- DAY HEADER -->
                <h2 class="schedule-day-title"><?= htmlspecialchars($day) ?></h2>

                <div class="schedule-columns-2">

                    <!-- MORNING (12 AM – 11:59 AM) -->
                    <div class="schedule-column">
                        <h3 class="schedule-period">🌅 Morning</h3>
                        <ul class="schedule-list">
                            <?php foreach ($morningSlots as $hour => $dj): ?>
                                <li class="<?= ($hour === $currentHourEST && $day === $currentDayEST ? 'live-now' : '') ?>">
                                    <span class="schedule-time"><?= displayHour($hour) ?></span>
                                    <span class="schedule-dj"><?= htmlspecialchars($dj) ?></span>

                                    <?php if ($hour === $currentHourEST && $day === $currentDayEST): ?>
                                        <span class="live-now-badge">LIVE NOW</span>
                                    <?php endif; ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>

                    <!-- AFTERNOON/EVENING (12 PM – 11:59 PM) -->
                    <div class="schedule-column">
                        <h3 class="schedule-period">🌙 Afternoon / Evening</h3>
                        <ul class="schedule-list">
                            <?php foreach ($aftEveSlots as $hour => $dj): ?>
                                <li class="<?= ($hour === $currentHourEST && $day === $currentDayEST ? 'live-now' : '') ?>">
                                    <span class="schedule-time"><?= displayHour($hour) ?></span>
                                    <span class="schedule-dj"><?= htmlspecialchars($dj) ?></span>

                                    <?php if ($hour === $currentHourEST && $day === $currentDayEST): ?>
                                        <span class="live-now-badge">LIVE NOW</span>
                                    <?php endif; ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>

                </div>
            </div>

            <div class="vinyl-divider center-divider"></div>

        <?php endforeach; ?>

        <!-- BACK BUTTON -->
        <div class="content-box center-text">
            <a href="index.php" class="button radio-static">⬅ Back to Home</a>
        </div>

        <?php include __DIR__ . '/includes/footer.php'; ?>
    </div>
</div>

<!-- Optional small styling for the (24:00) part -->
<style>
.time-24{
  opacity: .75;
  font-size: .92em;
  margin-left: 6px;
}
</style>