<?php
/**
 * File Riwayat Chat Admin - SMK Assistant
 * Monitoring interaksi Siswa & AI
 * Dikembangkan oleh: Sikawarsito, S.Kom
 */

session_start();
include '../config/database.php';

// 1. Proteksi Admin
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') { 
    header("Location: ../auth/login_page.php"); 
    exit; 
}

// 2. Logika Hapus Semua Riwayat (Jika dibutuhkan)
if (isset($_GET['aksi']) && $_GET['aksi'] == 'clear_all') {
    mysqli_query($conn, "DELETE FROM chat_history");
    header("Location: riwayat_chat.php?msg=cleared");
    exit;
}

// 3. Ambil Data Riwayat dengan Join ke tabel Users untuk mendapatkan Nama Siswa
$query_history = mysqli_query($conn, "
    SELECT h.*, u.nama 
    FROM chat_history h 
    JOIN users u ON h.user_id = u.id 
    ORDER BY h.created_at DESC
");
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Riwayat Interaksi | Admin Panel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        :root { --bg: #f8fafc; --primary: #0d6efd; --border: #e2e8f0; }
        body { font-family: 'Plus Jakarta Sans', sans-serif; background-color: var(--bg); color: #0f172a; }
        
        .admin-wrapper { display: flex; min-height: 100vh; }
        .main-content { flex: 1; padding: 40px; }
        
        /* Card & Table Style */
        .data-card { background: white; border: 1px solid var(--border); border-radius: 24px; overflow: hidden; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.02); }
        .table thead { background: #f8fafc; border-bottom: 1px solid var(--border); }
        .table thead th { padding: 16px 24px; color: #64748b; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 1px; }
        .table tbody td { padding: 20px 24px; vertical-align: top; border-bottom: 1px solid #f1f5f9; font-size: 0.9rem; }

        .chat-text { max-width: 300px; white-space: pre-wrap; font-size: 0.85rem; color: #475569; }
        .ai-badge { background: #eff6ff; color: #3b82f6; font-size: 0.7rem; font-weight: 700; padding: 4px 8px; border-radius: 6px; }
        .user-badge { background: #f1f5f9; color: #64748b; font-size: 0.7rem; font-weight: 700; padding: 4px 8px; border-radius: 6px; }
        
        .time-text { font-size: 0.75rem; color: #94a3b8; }
    </style>
</head>
<body>

<div class="admin-wrapper">
    <main class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-5">
            <div>
                <a href="index.php" class="text-decoration-none small fw-bold"><i class="bi bi-arrow-left"></i> Kembali ke Dashboard</a>
                <h2 class="fw-bold m-0 mt-2">Log Interaksi AI</h2>
                <p class="text-muted">Pantau percakapan antara siswa dan asisten kecerdasan buatan.</p>
            </div>
            <button onclick="confirmClear()" class="btn btn-outline-danger rounded-pill px-4 fw-bold">
                <i class="bi bi-trash3 me-2"></i> Hapus Semua Log
            </button>
        </div>

        <?php if(isset($_GET['msg']) && $_GET['msg'] == 'cleared'): ?>
            <div class="alert alert-success border-0 rounded-4 py-3 text-center mb-4 shadow-sm">
                <i class="bi bi-check-circle-fill me-2"></i> Semua riwayat pesan berhasil dibersihkan dari database.
            </div>
        <?php endif; ?>

        <div class="data-card">
            <div class="table-responsive">
                <table class="table mb-0">
                    <thead>
                        <tr>
                            <th>Siswa & Waktu</th>
                            <th>Pesan Siswa</th>
                            <th>Jawaban AI</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(mysqli_num_rows($query_history) > 0): ?>
                            <?php while($row = mysqli_fetch_assoc($query_history)): ?>
                            <tr>
                                <td>
                                    <div class="fw-bold text-dark"><?= htmlspecialchars($row['nama']) ?></div>
                                    <div class="time-text mt-1"><i class="bi bi-clock me-1"></i> <?= date('d M Y, H:i', strtotime($row['created_at'])) ?></div>
                                </td>
                                <td>
                                    <span class="user-badge mb-2 d-inline-block">USER</span>
                                    <div class="chat-text"><?= htmlspecialchars($row['user_message']) ?></div>
                                </td>
                                <td>
                                    <span class="ai-badge mb-2 d-inline-block">AI RESPONSE</span>
                                    <div class="chat-text text-primary" style="font-style: italic;">
                                        <?= (strlen($row['ai_response']) > 200) ? htmlspecialchars(substr($row['ai_response'], 0, 200)).'...' : htmlspecialchars($row['ai_response']) ?>
                                    </div>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="3" class="text-center py-5 text-muted italic">Belum ada riwayat percakapan.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="text-center mt-5">
            <small class="text-muted">Database: <b>genn5153_ai</b> &bull; Managed by <b>Sikawarsito, S.Kom</b></small>
        </div>
    </main>
</div>

<script>
function confirmClear() {
    if(confirm('Apakah Anda yakin ingin menghapus SELURUH riwayat pesan? Tindakan ini tidak dapat dibatalkan.')) {
        window.location.href = 'riwayat_chat.php?aksi=clear_all';
    }
}
</script>

</body>
</html>