<?php
require __DIR__ . '/_auth.php';
requireLogin();

$db = db();

// Hantera snabb-toggle aktiv/inaktiv
if (isset($_GET['toggle'])) {
    $db->prepare("UPDATE stations SET active = 1-active WHERE id=?")->execute([intval($_GET['toggle'])]);
    header('Location: index.php'); exit;
}

$stations = $db->query("
    SELECT s.*,
           COUNT(DISTINCT st.id) AS stream_count,
           SUM(CASE WHEN ss.online=1 THEN 1 ELSE 0 END) AS online_count,
           (SELECT MAX(started_at) FROM play_log WHERE station_id=s.id) AS last_play
    FROM stations s
    LEFT JOIN streams st ON st.station_id=s.id AND st.active=1
    LEFT JOIN stream_status ss ON ss.stream_id=st.id
    GROUP BY s.id
    ORDER BY s.sort_order, s.name
")->fetchAll(PDO::FETCH_ASSOC);

adminHeader('Kanalhantering');

$ok  = flash('ok');
$err = flash('err');
if ($ok)  echo "<div class='alert alert-success'>$ok</div>";
if ($err) echo "<div class='alert alert-error'>$err</div>";
?>

<div style="display:flex;justify-content:flex-end;margin-bottom:16px">
  <a href="add.php" class="btn btn-primary">+ Ny kanal</a>
</div>

<table class="admin-table">
<thead>
  <tr>
    <th style="width:40px">#</th>
    <th>Kanal</th>
    <th>Genre</th>
    <th>Streams</th>
    <th>Status</th>
    <th>Senast spelad</th>
    <th>Åtgärder</th>
  </tr>
</thead>
<tbody>
<?php foreach ($stations as $s): ?>
<tr>
  <td><?= $s['sort_order'] ?></td>
  <td>
    <strong><?= htmlspecialchars($s['name']) ?></strong><br>
    <small style="color:var(--muted)"><?= htmlspecialchars($s['slug']) ?></small>
  </td>
  <td><?= htmlspecialchars($s['genre'] ?? '–') ?></td>
  <td>
    <span class="badge <?= $s['online_count'] > 0 ? 'badge-green' : ($s['stream_count'] > 0 ? 'badge-red' : 'badge-gray') ?>">
      <?= $s['online_count'] ?>/<?= $s['stream_count'] ?> online
    </span>
  </td>
  <td>
    <?php if ($s['active']): ?>
      <span class="badge badge-green">Aktiv</span>
    <?php else: ?>
      <span class="badge badge-red">Dold</span>
    <?php endif; ?>
  </td>
  <td style="font-size:.8rem;color:var(--muted)">
    <?= $s['last_play'] ? date('d/m H:i', strtotime($s['last_play'])) : '–' ?>
  </td>
  <td>
    <a href="edit.php?id=<?= $s['id'] ?>" class="btn btn-secondary" style="padding:4px 12px;font-size:.82rem">Redigera</a>
    <a href="index.php?toggle=<?= $s['id'] ?>" class="btn btn-secondary" style="padding:4px 12px;font-size:.82rem"
       title="<?= $s['active'] ? 'Dölj' : 'Aktivera' ?>">
      <?= $s['active'] ? '👁 Dölj' : '✓ Visa' ?>
    </a>
    <a href="delete.php?id=<?= $s['id'] ?>" class="btn btn-danger" style="padding:4px 12px;font-size:.82rem"
       onclick="return confirm('Ta bort <?= addslashes($s['name']) ?>?')">✕</a>
  </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<div style="margin-top:24px;color:var(--muted);font-size:.82rem">
  <p>💡 Kör <code>php workers/probe.php</code> för att uppdatera stream-status</p>
  <p>💡 Cron-exempel:<br>
     <code>*/5 * * * * php /var/www/radio/workers/probe.php</code><br>
     <code>* * * * *   php /var/www/radio/workers/metadata.php</code><br>
     <code>0 3 * * *   php /var/www/radio/workers/cleanup.php</code>
  </p>
</div>

<?php adminFooter(); ?>
