<?php
session_start();
require __DIR__ . '/auth.php';
$pageTitle = 'Live Chat — Admin';
require __DIR__ . '/includes/admin_header.php';

$settingsFile = __DIR__ . '/../data/settings.json';
$settings = [];
if (file_exists($settingsFile)) {
  $settings = json_decode(file_get_contents($settingsFile), true);
  if (!is_array($settings)) $settings = [];
}

function save_settings($file, $settings){
  file_put_contents($file, json_encode($settings, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
}

$chatFile = __DIR__ . '/../data/chat.json';
if (!file_exists($chatFile)) file_put_contents($chatFile, '[]');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (isset($_POST['save_chat_settings'])) {
    $settings['chat_enabled'] = !empty($_POST['chat_enabled']);
    $settings['chat_live_now'] = !empty($_POST['chat_live_now']);
    $settings['chat_show_in_nav'] = !empty($_POST['chat_show_in_nav']);
    save_settings($settingsFile, $settings);
    header('Location: chat.php?saved=1'); exit;
  }

  if (isset($_POST['clear_chat'])) {
    file_put_contents($chatFile, '[]');
    header('Location: chat.php?cleared=1'); exit;
  }

  if (isset($_POST['admin_send'])) {
    $msg = trim((string)($_POST['msg'] ?? ''));
    $name = trim((string)($_POST['name'] ?? 'DJ'));
    if ($msg !== '') {
      $data = json_decode(file_get_contents($chatFile), true);
      if (!is_array($data)) $data = [];
      $data[] = ['role'=>'admin','name'=>$name,'msg'=>$msg,'time'=>date('H:i'),'ts'=>time()];
      if (count($data)>200) $data = array_slice($data, -200);
      file_put_contents($chatFile, json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
    }
    header('Location: chat.php?sent=1'); exit;
  }
}

$chatEnabled = !empty($settings['chat_enabled']);
$chatLiveNow = !empty($settings['chat_live_now']);
$chatShowNav = array_key_exists('chat_show_in_nav', $settings) ? !empty($settings['chat_show_in_nav']) : true;

?>
<div class="admin-wrap">
  <a class="btn btn-accent" href="/admin/index.php">← Return to Admin</a>

  <div class="content-card">
    <h1 style="margin:0;">💬 Live Chat</h1>
    <div style="opacity:.85;">Enable/disable chat, show/hide the nav link, and post as admin.</div>
  </div>

  <?php if (!empty($_GET['saved'])): ?><div class="msg ok">Saved.</div><?php endif; ?>
  <?php if (!empty($_GET['cleared'])): ?><div class="msg ok">Chat cleared.</div><?php endif; ?>
  <?php if (!empty($_GET['sent'])): ?><div class="msg ok">Message sent.</div><?php endif; ?>

  <div class="content-card" style="text-align:left;">
    <form method="post">
      <input type="hidden" name="save_chat_settings" value="1">
      <label style="display:block;margin:10px 0;">
        <input type="checkbox" name="chat_enabled" <?= $chatEnabled?'checked':''; ?>> Enable chat feature
      </label>
      <label style="display:block;margin:10px 0;">
        <input type="checkbox" name="chat_show_in_nav" <?= $chatShowNav?'checked':''; ?>> Show chat link in navigation (when live)
      </label>
      <label style="display:block;margin:10px 0;">
        <input type="checkbox" name="chat_live_now" <?= $chatLiveNow?'checked':''; ?>> Live DJ on-air (show chat link)
      </label>

      <button class="btn btn-accent" type="submit">Save Settings</button>
    </form>
  </div>

  <div class="content-card" style="text-align:left;">
    <h2 style="margin-top:0;">Post as Admin</h2>
    <form method="post">
      <input type="hidden" name="admin_send" value="1">
      <div class="request-field">
        <label>Display name</label>
        <input class="request-input" type="text" name="name" value="DJ Smurfette" maxlength="32">
      </div>
      <div class="request-field">
        <label>Message</label>
        <input class="request-input" type="text" name="msg" maxlength="240" placeholder="Type a message…">
      </div>
      <button class="btn btn-accent" type="submit">Send</button>
    </form>
  </div>

  <div class="content-card" style="text-align:left;">
    <h2 style="margin-top:0;">Moderation</h2>
    <form method="post" onsubmit="return confirm('Clear chat for everyone?');">
      <input type="hidden" name="clear_chat" value="1">
      <button class="btn btn-accent" type="submit">Clear Chat</button>
    </form>
  </div>

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

<?php include __DIR__ . '/includes/admin_footer.php'; ?>
