<?php include('../header.php'); ?>
<?php include('../menu.php'); ?>

<?php
$scheduleFile = '../data/schedule.txt';
$schedule = [];

if (file_exists($scheduleFile)) {
  $lines = file($scheduleFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_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;
    }
  }
}

/* 12-hour 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;
}

/* 24-hour format (normalized) */
function formatHour24($hour24) {
  $h = (int)substr($hour24, 0, 2);
  return str_pad((string)$h, 2, '0', STR_PAD_LEFT) . ':00';
}

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

/* Sort slots by hour */
function sortSlotsByHour(&$arr) {
  uksort($arr, function($a, $b) {
    return ((int)substr($a, 0, 2)) <=> ((int)substr($b, 0, 2));
  });
}
?>

<main class="main-container">
  <section class="content-box">
    <h2 class="text-warning">📅 DJ Schedule</h2>
    <p class="text-muted mb-3">Here’s who’s spinning thunder across the week. All times are Eastern Standard (12-hour + 24-hour format).</p>

    <?php foreach ($schedule as $day => $slots): ?>

      <?php
        // Break into 3 sections:
        // Morning: 00–11
        // Afternoon: 12–17
        // Evening: 18–23
        $morning = [];
        $afternoon = [];
        $evening = [];

        foreach ($slots as $hour => $dj) {
          $h = (int)substr($hour, 0, 2);
          if ($h < 12) {
            $morning[$hour] = $dj;
          } elseif ($h < 18) {
            $afternoon[$hour] = $dj;
          } else {
            $evening[$hour] = $dj;
          }
        }

        // Sort each bucket so times are in the right order
        sortSlotsByHour($morning);
        sortSlotsByHour($afternoon);
        sortSlotsByHour($evening);
      ?>

      <div class="content-box mb-4">
        <h3 class="text-warning"><?= htmlspecialchars($day) ?></h3>

        <div class="schedule-grid">
          <div class="schedule-column">
            <h5 class="text-muted">🌅 Morning</h5>
            <ul class="schedule-list">
              <?php foreach ($morning as $hour => $dj): ?>
                <li><strong><?= displayHour($hour) ?></strong> — <?= htmlspecialchars($dj) ?></li>
              <?php endforeach; ?>
            </ul>
          </div>

          <div class="schedule-column">
            <h5 class="text-muted">🌇 Afternoon</h5>
            <ul class="schedule-list">
              <?php foreach ($afternoon as $hour => $dj): ?>
                <li><strong><?= displayHour($hour) ?></strong> — <?= htmlspecialchars($dj) ?></li>
              <?php endforeach; ?>
            </ul>
          </div>

          <div class="schedule-column">
            <h5 class="text-muted">🌙 Evening</h5>
            <ul class="schedule-list">
              <?php foreach ($evening as $hour => $dj): ?>
                <li><strong><?= displayHour($hour) ?></strong> — <?= htmlspecialchars($dj) ?></li>
              <?php endforeach; ?>
            </ul>
          </div>
        </div>
      </div>
    <?php endforeach; ?>
  </section>
</main>

<!-- Small styling for the 24h time -->
<style>
.time-24{
  opacity: .75;
  font-size: .92em;
  margin-left: 6px;
}
</style>

<?php include('../footer.php'); ?>