<?php include 'includes/header.php'; ?>
<?php include 'includes/menu.php'; ?>

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

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;
      $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 */
function displayHour($hour24) {
  return formatHour12($hour24) . ' <small class="text-muted">(' . formatHour24($hour24) . ')</small>';
}

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

<div class="container mt-5">
  <div class="content-box">
    <h2>📅 DJ Schedule</h2>
    <p class="mb-4">Here’s who’s spinning thunder across the week. All times are Eastern Standard (12-hour + 24-hour).</p>

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

      <?php
        // Split into 3 buckets
        $morning = [];   // 00–11
        $afternoon = []; // 12–17
        $evening = [];   // 18–23

        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 section
        sortSlotsByHour($morning);
        sortSlotsByHour($afternoon);
        sortSlotsByHour($evening);
      ?>

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

        <div class="row">
          <!-- Morning -->
          <div class="col-md-4">
            <h6 class="text-muted">🌅 Morning</h6>
            <ul class="list-unstyled">
              <?php foreach ($morning as $hour => $dj): ?>
                <li><strong><?= displayHour($hour) ?></strong> — <?= htmlspecialchars($dj) ?></li>
              <?php endforeach; ?>
            </ul>
          </div>

          <!-- Afternoon -->
          <div class="col-md-4">
            <h6 class="text-muted">🌇 Afternoon</h6>
            <ul class="list-unstyled">
              <?php foreach ($afternoon as $hour => $dj): ?>
                <li><strong><?= displayHour($hour) ?></strong> — <?= htmlspecialchars($dj) ?></li>
              <?php endforeach; ?>
            </ul>
          </div>

          <!-- Evening -->
          <div class="col-md-4">
            <h6 class="text-muted">🌙 Evening</h6>
            <ul class="list-unstyled">
              <?php foreach ($evening as $hour => $dj): ?>
                <li><strong><?= displayHour($hour) ?></strong> — <?= htmlspecialchars($dj) ?></li>
              <?php endforeach; ?>
            </ul>
          </div>
        </div>

      </div>
    <?php endforeach; ?>

  </div>
</div>

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