<?php
// index.php - Main Dashboard
session_start();
require_once 'db/config.php';
require_once 'languages/helper.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$user_id = $_SESSION['user_id'];
$stmt = db()->prepare("SELECT username, role, ai_personality FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

if (!$user) {
    session_destroy();
    header("Location: login.php");
    exit();
}

$is_admin = ($user['role'] ?? 'user') === 'admin';
$ai_personality = $user['ai_personality'] ?? 'Expert';

// Simple SEO meta
$page_title = __('welcome') . " - AI Assistant";
$meta_description = "AI Dashboard for TikTok automation and more.";
?>
<!DOCTYPE html>
<html lang="<?= $lang ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $page_title ?></title>
    <meta name="description" content="<?= $meta_description ?>">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Custom CSS -->
    <link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
    <style>
      /* Minor overrides for layout */
      .ai-avatar {
          width: 140px;
          height: 140px;
          border-radius: 50%;
          border: 4px solid var(--tiktok-cyan);
          overflow: hidden;
          background: #000;
          box-shadow: 0 0 20px rgba(0, 242, 234, 0.4);
          margin: 0 auto;
      }
      .ai-avatar img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          border-radius: 50%;
      }
      .status-dot.pulse {
          animation: pulse-animation 1.5s infinite;
      }
      @keyframes pulse-animation {
          0% { transform: scale(1); opacity: 1; }
          50% { transform: scale(1.2); opacity: 0.5; }
          100% { transform: scale(1); opacity: 1; }
      }
    </style>
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark bg-surface shadow-sm sticky-top">
  <div class="container">
    <a class="navbar-brand fw-bold" href="index.php">
      <span class="text-tiktok-red">AI</span> <span class="text-tiktok-cyan">DASHBOARD</span>
    </a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav me-auto">
        <li class="nav-item">
          <a class="nav-link active" href="index.php"><i class="fa-solid fa-house me-1"></i> Dashboard</a>
        </li>
        <?php if ($is_admin): ?>
        <li class="nav-item">
          <a class="nav-link" href="admin.php"><i class="fa-solid fa-user-shield me-1"></i> Admin</a>
        </li>
        <?php endif; ?>
      </ul>
      <div class="navbar-nav align-items-center">
        <span class="nav-item me-3 text-secondary small">
          <i class="fa-solid fa-user me-1"></i> <?= htmlspecialchars($user['username']) ?>
        </span>
        <div class="nav-item dropdown me-2">
            <a class="nav-link dropdown-toggle" href="#" id="langDropdown" role="button" data-bs-toggle="dropdown">
                <i class="fa-solid fa-globe"></i>
            </a>
            <ul class="dropdown-menu dropdown-menu-end bg-surface border-secondary shadow">
                <li><a class="dropdown-item text-light" href="?lang=id">Indonesia</a></li>
                <li><a class="dropdown-item text-light" href="?lang=en">English</a></li>
            </ul>
        </div>
        <a href="logout.php" class="btn btn-outline-danger btn-sm rounded-pill px-3">
          <i class="fa-solid fa-right-from-bracket me-1"></i> Logout
        </a>
      </div>
    </div>
  </div>
</nav>

<main class="container py-5">
  <div class="text-center mb-5">
    <h1 class="display-5 fw-bold mb-2"><?= __('welcome') ?>, <span class="text-tiktok-red"><?= htmlspecialchars($user['username']) ?></span>!</h1>
    <p class="lead text-secondary"><?= __('hero_text') ?></p>
  </div>

  <div class="row g-4">
    <!-- Left Column: Controls & AI personality -->
    <div class="col-lg-4">
      <!-- AI Avatar Display -->
      <div class="card bg-surface border-secondary mb-4 shadow-sm py-4">
        <div class="card-body text-center">
          <div class="ai-avatar mb-3">
             <img src="assets/images/ai_avatar.jpg?v=<?php echo time(); ?>" alt="AI Avatar">
          </div>
          <h5 class="fw-bold text-tiktok-cyan mb-1"><?= __('welcome') ?></h5>
          <div id="connectionStatus" class="small text-secondary mb-3">
            <span class="status-dot bg-secondary me-1"></span> Disconnected
          </div>
          <hr class="border-secondary opacity-25">
          <div class="text-start px-3">
            <label class="form-label text-secondary small fw-bold mb-1"><?= __('personality') ?></label>
            <select class="form-select bg-dark text-light border-secondary" id="aiPersonality">
                <option value="Funny" <?= $ai_personality == 'Funny' ? 'selected' : '' ?>><?= __('personality_funny') ?></option>
                <option value="Serious" <?= $ai_personality == 'Serious' ? 'selected' : '' ?>><?= __('personality_serious') ?></option>
                <option value="Expert" <?= $ai_personality == 'Expert' ? 'selected' : '' ?>><?= __('personality_expert') ?></option>
            </select>
          </div>
        </div>
      </div>

      <!-- Config Card -->
      <div class="card bg-surface border-secondary mb-4 shadow-sm">
        <div class="card-body">
          <h5 class="card-title mb-4 fw-bold"><i class="fa-brands fa-tiktok me-2"></i> <?= __('config') ?></h5>
          
          <div class="mb-3">
            <label for="tiktokUsername" class="form-label text-secondary small fw-bold"><?= __('tiktok_username') ?></label>
            <div class="input-group">
                <span class="input-group-text bg-dark border-secondary text-secondary">@</span>
                <input type="text" class="form-control bg-dark text-light border-secondary" id="tiktokUsername" placeholder="username">
            </div>
            <div class="form-text text-secondary mt-1 small">Enter your TikTok username that is currently live.</div>
          </div>

          <div class="d-grid gap-2">
            <button class="btn btn-tiktok-cyan text-dark fw-bold" id="connectBtn">
              <i class="fa-solid fa-plug me-2"></i> <?= __('connect') ?>
            </button>
            <button class="btn btn-outline-danger fw-bold d-none" id="disconnectBtn">
              <i class="fa-solid fa-power-off me-2"></i> <?= __('disconnect') ?>
            </button>
          </div>
        </div>
      </div>

      <!-- TTS Settings -->
      <div class="card bg-surface border-secondary shadow-sm">
        <div class="card-body">
          <h5 class="card-title mb-3 fw-bold"><i class="fa-solid fa-volume-high me-2"></i> <?= __('tts_settings') ?></h5>
          <div class="mb-3">
            <label class="form-label text-secondary small fw-bold"><?= __('voice') ?></label>
            <select class="form-select bg-dark text-light border-secondary" id="voiceSelect"></select>
          </div>
          <div class="mb-3">
            <label class="form-label text-secondary small fw-bold d-flex justify-content-between">
              <span><?= __('speed') ?></span>
              <span id="rateValue" class="text-tiktok-cyan">1.0</span>
            </label>
            <input type="range" class="form-range" id="rateRange" min="0.5" max="2" step="0.1" value="1">
          </div>
          <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" id="autoReplyToggle" checked>
            <label class="form-check-label text-secondary small fw-bold" for="autoReplyToggle"><?= __('auto_reply') ?></label>
          </div>
        </div>
      </div>
    </div>

    <!-- Right Column: Live Feed & History -->
    <div class="col-lg-8">
      <!-- Comment Feed -->
      <div class="card bg-dark border-secondary mb-4 shadow-sm" style="min-height: 500px;">
        <div class="card-header bg-surface border-secondary d-flex justify-content-between align-items-center py-3">
          <h5 class="mb-0 fw-bold"><i class="fa-solid fa-stream me-2 text-tiktok-cyan"></i> <?= __('live_feed') ?></h5>
          <span class="badge bg-tiktok-red" id="commentCount">0 <?= __('comments') ?></span>
        </div>
        <div class="card-body p-0 d-flex flex-column">
          <div id="commentFeed" class="p-3 flex-grow-1" style="height: 400px; overflow-y: auto;">
            <div id="emptyFeed" class="text-center py-5 opacity-50">
                <i class="fa-solid fa-satellite-dish fa-3x mb-3 text-secondary"></i>
                <p><?= __('no_history') ?></p>
            </div>
          </div>
          <div class="p-3 bg-surface border-top border-secondary mt-auto">
            <div class="input-group">
                <input type="text" class="form-control bg-dark text-light border-secondary" id="manualComment" placeholder="Simulate a comment...">
                <button class="btn btn-outline-tiktok-cyan fw-bold" id="simulateBtn"><?= __('simulate') ?></button>
            </div>
          </div>
        </div>
      </div>

      <!-- History -->
      <div class="card bg-surface border-secondary shadow-sm">
        <div class="card-header bg-transparent border-secondary py-3">
            <h5 class="mb-0 fw-bold"><i class="fa-solid fa-history me-2 text-secondary"></i> <?= __('recent_interactions') ?></h5>
        </div>
        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr class="border-secondary text-secondary">
                        <th class="small fw-bold"><?= __('time') ?></th>
                        <th class="small fw-bold"><?= __('user') ?></th>
                        <th class="small fw-bold"><?= __('comment') ?></th>
                        <th class="small fw-bold"><?= __('ai_reply') ?></th>
                    </tr>
                </thead>
                <tbody id="historyTableBody">
                    <tr id="noHistoryRow">
                        <td colspan="4" class="text-center py-4 text-secondary"><?= __('no_history') ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
      </div>
    </div>
  </div>
</main>

<footer class="py-4 mt-5 border-top border-secondary">
  <div class="container text-center">
    <p class="text-secondary small mb-0"><?= __('footer_text') ?></p>
  </div>
</footer>

<!-- Toast Container -->
<div id="toastContainer" class="toast-container position-fixed bottom-0 end-0 p-3" style="z-index: 1100"></div>

<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>