<?php
declare(strict_types=1);
require_once __DIR__ . '/db/config.php';

// Fetch settings
$settings = [];
try {
    $stmt = db()->query("SELECT setting_key, setting_value FROM bot_settings");
    while ($row = $stmt->fetch()) {
        $settings[$row['setting_key']] = $row['setting_value'];
    }
} catch (PDOException $e) {
    // Table might not exist yet or other DB error
}

$discordToken = $settings['discord_token'] ?? '';
$voiceChannelId = $settings['voice_channel_id'] ?? '1457687430189682781';
$sahurTime = $settings['sahur_time'] ?? '03:30';

$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Discord Bot for Sahur Alarm and Music';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Sahur Bot Command Center</title>
  <meta name="description" content="<?= htmlspecialchars($projectDescription) ?>" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
</head>
<body>
  <div class="container">
    <header class="header">
      <div>
        <h1>Sahur Bot</h1>
        <p style="margin:0; color:var(--text-muted); font-size: 0.875rem;">Command Center</p>
      </div>
      <div id="bot-status-container">
        <span id="bot-status-badge" class="badge badge-disconnected">Checking...</span>
      </div>
    </header>

    <div class="card">
      <h2 class="card-title">Bot Configuration</h2>
      <form id="config-form">
        <div class="form-group">
          <label class="form-label" for="discord_token">Discord Bot Token</label>
          <input type="password" id="discord_token" name="discord_token" class="form-control" placeholder="MTAx..." value="<?= htmlspecialchars($discordToken) ?>">
        </div>
        <div class="form-group">
          <label class="form-label" for="client_id">Discord Client ID (Application ID)</label>
          <input type="text" id="client_id" name="client_id" class="form-control" placeholder="101..." value="<?= htmlspecialchars($settings['client_id'] ?? '') ?>">
          <small style="color:var(--text-muted); font-size: 0.75rem;">Dibutuhkan untuk mendaftarkan Slash Commands.</small>
        </div>
        <div class="form-group">
          <label class="form-label" for="voice_channel_id">Voice Channel ID</label>
          <input type="text" id="voice_channel_id" name="voice_channel_id" class="form-control" placeholder="1457..." value="<?= htmlspecialchars($voiceChannelId) ?>">
        </div>
        <div class="form-group">
          <label class="form-label" for="sahur_time">Alarm Time (WIB)</label>
          <input type="time" id="sahur_time" name="sahur_time" class="form-control" value="<?= htmlspecialchars($sahurTime) ?>">
        </div>
        <div style="display: flex; gap: 10px; flex-wrap: wrap;">
          <button type="submit" class="btn btn-primary" style="flex: 1; min-width: 150px;">Save Settings</button>
          <button type="button" id="deploy-commands-btn" class="btn" style="border: 1px solid var(--border); background: #fff; flex: 1; min-width: 150px;">Deploy Slash Commands</button>
          <button type="button" id="start-bot-btn" class="btn" style="border: 1px solid var(--primary); color: var(--primary); background: #fff; flex: 1; min-width: 150px;">Start/Restart Bot</button>
        </div>
      </form>
    </div>

    <div class="card">
      <h2 class="card-title">Sound Preview</h2>
      <p style="color:var(--text-muted); font-size: 0.875rem; margin-bottom: 12px;">This sound will be played in the voice channel at <?= htmlspecialchars($sahurTime) ?> WIB.</p>
      
      <?php 
      $audioFile = 'assets/audio/sahur.mp3';
      $exists = file_exists(__DIR__ . '/' . $audioFile);
      $lastUpdated = $exists ? date("d M Y, H:i", filemtime(__DIR__ . '/' . $audioFile)) : 'Never';
      ?>
      
      <div id="audio-status" style="margin-bottom: 12px; padding: 10px; background: <?= $exists ? '#f0fdf4' : '#fff1f2' ?>; border-radius: 8px; font-size: 0.875rem; display: flex; align-items: center; gap: 8px; border: 1px solid <?= $exists ? '#bbf7d0' : '#fecaca' ?>;">
        <span style="font-size: 1.25rem;"><?= $exists ? '✅' : '❌' ?></span>
        <div>
          <strong>Status: <?= $exists ? 'Audio Tersimpan' : 'Audio Belum Ada' ?></strong><br>
          <span style="color: var(--text-muted); font-size: 0.75rem;">Terakhir diupdate: <?= $lastUpdated ?></span>
        </div>
      </div>

      <audio controls class="audio-player" style="width: 100%;">
        <source src="<?= $audioFile ?>?v=<?= $exists ? filemtime(__DIR__ . '/' . $audioFile) : time() ?>" type="audio/mpeg">
        Your browser does not support the audio element.
      </audio>
      
      <div style="margin-top: 16px; display: flex; flex-direction: column; gap: 12px;">
        <button id="test-audio-btn" class="btn" style="border: 1px solid var(--border);">Test Play Locally</button>
        
        <div style="padding: 16px; background: #f8fafc; border: 1px dashed var(--border); border-radius: var(--radius);">
          <label class="form-label" style="margin-bottom: 8px; display: block; font-weight: 600;">Update Sahur MP3</label>
          <p style="font-size: 0.75rem; color: var(--text-muted); margin-bottom: 8px;">Upload file baru untuk mengganti yang sudah ada.</p>
          <input type="file" id="sahur-upload" accept="audio/mpeg" style="font-size: 0.75rem; width: 100%;">
          <button id="upload-btn" class="btn btn-primary" style="margin-top: 10px; width: 100%; display: none;">Upload & Simpan</button>
          <div id="upload-status" style="margin-top: 8px; font-size: 0.75rem;"></div>
        </div>
      </div>
    </div>

    <div class="card">
      <h2 class="card-title">Bot Code Status</h2>
      <p style="color:var(--text-muted); font-size: 0.875rem;">The <code>discord.js v14</code> bot skeleton has been generated in the <code>/bot</code> directory.</p>
      <div style="background: #f1f5f9; padding: 12px; border-radius: var(--radius); font-family: monospace; font-size: 0.75rem; color: #334155; margin-top: 8px;">
        # To start the bot:<br>
        cd bot<br>
        npm install<br>
        DISCORD_TOKEN="your_token" node index.js
      </div>
    </div>

    <footer style="margin-top: 48px; text-align: center; color: var(--text-muted); font-size: 0.75rem;">
      &copy; <?= date('Y') ?> Sahur Bot &bull; Powered by Flatlogic LAMP
    </footer>
  </div>

  <script src="assets/js/main.js?v=<?= time() ?>"></script>
</body>
</html>
