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

$db = db();
$settings = $db->query("SELECT setting_key, setting_value FROM bot_settings")->fetchAll(PDO::FETCH_KEY_PAIR);
$logs = $db->query("SELECT * FROM bot_logs ORDER BY created_at DESC LIMIT 10")->fetchAll();

$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Discord Sahur Bot Management Dashboard';
$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.0">
    <title>Sahur Bot Dashboard</title>
    <?php if ($projectDescription): ?>
        <meta name="description" content="<?= htmlspecialchars($projectDescription) ?>" />
        <meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
        <meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
    <?php endif; ?>
    <?php if ($projectImageUrl): ?>
        <meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
        <meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
    <?php endif; ?>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <link href="assets/css/custom.css?v=<?= time() ?>" rel="stylesheet">
    <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">
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">
    <div class="container">
        <a class="navbar-brand" href="#"><i class="bi bi-robot"></i> Sahur Bot</a>
        <div class="ms-auto">
            <span class="badge <?= ($settings['bot_status'] ?? 'offline') === 'online' ? 'bg-success' : 'bg-danger' ?>">
                <i class="bi bi-circle-fill me-1"></i> <?= strtoupper($settings['bot_status'] ?? 'offline') ?>
            </span>
        </div>
    </div>
</nav>

<div class="container">
    <div class="row">
        <!-- Configuration Section -->
        <div class="col-lg-8">
            <div class="card mb-4 shadow-sm">
                <div class="card-header bg-white">
                    <h5 class="mb-0 font-weight-bold">Bot Configuration</h5>
                </div>
                <div class="card-body">
                    <form id="configForm" action="save_config.php" method="POST">
                        <div class="mb-3">
                            <label for="bot_token" class="form-label">Discord Bot Token</label>
                            <input type="password" class="form-control" id="bot_token" name="bot_token" value="<?= htmlspecialchars($settings['bot_token'] ?? '') ?>" placeholder="Enter your bot token">
                            <div class="form-text text-muted">Never share your token with anyone.</div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="voice_channel_id" class="form-label">Voice Channel ID</label>
                                <input type="text" class="form-control" id="voice_channel_id" name="voice_channel_id" value="<?= htmlspecialchars($settings['voice_channel_id'] ?? '') ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="sahur_time" class="form-label">Sahur Time (HH:MM)</label>
                                <input type="time" class="form-control" id="sahur_time" name="sahur_time" value="<?= htmlspecialchars($settings['sahur_time'] ?? '03:00') ?>" required>
                            </div>
                        </div>
                        <div class="mb-3">
                            <label for="sahur_source" class="form-label">Sahur Audio Source (URL or File Path)</label>
                            <input type="text" class="form-control" id="sahur_source" name="sahur_source" value="<?= htmlspecialchars($settings['sahur_source'] ?? 'sahur.mp3') ?>" required>
                        </div>
                        <div class="d-flex justify-content-between">
                            <button type="submit" class="btn btn-primary">Save Configuration</button>
                            <button type="button" id="btnTestSahur" class="btn btn-outline-secondary">Test Sahur Now</button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Bot Status & Actions -->
            <div class="card mb-4 shadow-sm">
                <div class="card-header bg-white">
                    <h5 class="mb-0">Bot Management</h5>
                </div>
                <div class="card-body">
                    <p>Manage the Discord bot service status.</p>
                    <div class="d-grid gap-2 d-md-block">
                        <button id="btnStartBot" class="btn btn-success me-md-2" type="button"><i class="bi bi-play-fill"></i> Start Bot</button>
                        <button id="btnStopBot" class="btn btn-danger me-md-2" type="button"><i class="bi bi-stop-fill"></i> Stop Bot</button>
                        <button id="btnRestartBot" class="btn btn-warning" type="button"><i class="bi bi-arrow-clockwise"></i> Restart</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- Logs Section -->
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header bg-white d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Activity Logs</h5>
                    <button class="btn btn-sm btn-link p-0" id="refreshLogs"><i class="bi bi-arrow-repeat"></i></button>
                </div>
                <div class="card-body p-0">
                    <div class="list-group list-group-flush" id="logList">
                        <?php if (empty($logs)): ?>
                            <div class="p-3 text-center text-muted">No activity logs found.</div>
                        <?php else: ?>
                            <?php foreach ($logs as $log): ?>
                                <div class="list-group-item">
                                    <div class="d-flex w-100 justify-content-between">
                                        <small class="text-muted"><?= $log['created_at'] ?></small>
                                        <span class="badge bg-<?= $log['log_level'] === 'error' ? 'danger' : ($log['log_level'] === 'warning' ? 'warning' : 'info') ?>"><?= $log['log_level'] ?></span>
                                    </div>
                                    <p class="mb-1 small"><?= htmlspecialchars($log['message']) ?></p>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<footer class="mt-5 py-4 bg-light border-top">
    <div class="container text-center text-muted">
        <small>&copy; <?= date('Y') ?> Sahur Discord Bot Dashboard. Built with LAMP.</small>
    </div>
</footer>

<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
    <div id="liveToast" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
            <i class="bi bi-info-circle me-2"></i>
            <strong class="me-auto">Notification</strong>
            <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
        <div class="toast-body" id="toastMessage"></div>
    </div>
</div>

<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=<?= time() ?>"></script>
</body>
</html>
