<?php
/**
 * E-Book SMA — Database Installer
 * Akses sekali di: http://localhost/APIII/install.php
 * HAPUS file ini setelah database berhasil dibuat!
 */

$host = 'localhost';
$user = 'root';
$pass = '';  // Ganti jika MySQL Anda memiliki password
$db   = 'ebook_sma';

$conn = new mysqli($host, $user, $pass);
if ($conn->connect_error) {
    die('<div style="color:red;font-family:monospace;padding:20px">❌ Koneksi gagal: ' . $conn->connect_error . '<br><br>Pastikan XAMPP MySQL sudah berjalan dan sesuaikan password di baris $pass pada file ini.</div>');
}

$sqls = [
    "CREATE DATABASE IF NOT EXISTS `ebook_sma` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci",
    "USE `ebook_sma`",
    "CREATE TABLE IF NOT EXISTS `users` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `nama` varchar(100) NOT NULL,
        `email` varchar(100) NOT NULL,
        `password` varchar(255) NOT NULL,
        `role` enum('admin','user') NOT NULL DEFAULT 'user',
        `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        UNIQUE KEY `email` (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
    "CREATE TABLE IF NOT EXISTS `ebooks` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `judul` varchar(200) NOT NULL,
        `penulis` varchar(100) NOT NULL,
        `kategori` varchar(50) NOT NULL,
        `deskripsi` text DEFAULT NULL,
        `file` varchar(255) NOT NULL,
        `cover` varchar(255) DEFAULT NULL,
        `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
    // admin@ebook.com / password  &  user@ebook.com / password
    "INSERT IGNORE INTO `users` (`nama`, `email`, `password`, `role`) VALUES
        ('Administrator', 'admin@ebook.com', '\$2y\$10\$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin'),
        ('Budi Santoso', 'user@ebook.com', '\$2y\$10\$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'user')",
];

$conn->select_db($db);
$errors = [];
foreach ($sqls as $sql) {
    if (!$conn->query($sql)) {
        // Skip USE db error (already selected)
        if (strpos($sql, 'USE') !== 0) {
            $errors[] = $conn->error . ' → ' . substr($sql, 0, 60) . '...';
        }
    }
}

$conn->close();
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>E-Book SMA Installer</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
<style>
  * { box-sizing: border-box; margin: 0; padding: 0; }
  body { font-family: 'Inter', sans-serif; background: #f1f5f9; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
  .card { background: white; border-radius: 16px; padding: 40px; max-width: 520px; width: 100%; box-shadow: 0 8px 40px rgba(0,0,0,.1); }
  .logo { width: 60px; height: 60px; background: #4f46e5; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 26px; color: white; margin-bottom: 20px; }
  h1 { font-size: 22px; font-weight: 700; color: #0f172a; margin-bottom: 8px; }
  p { font-size: 14px; color: #64748b; line-height: 1.6; margin-bottom: 20px; }
  .status { padding: 14px 18px; border-radius: 10px; margin-bottom: 14px; font-size: 14px; }
  .ok   { background: #d1fae5; color: #065f46; }
  .err  { background: #fee2e2; color: #991b1b; }
  .warn { background: #fef3c7; color: #92400e; font-size: 13px; margin-top: 20px; padding: 12px 16px; }
  .btn-go { display: inline-block; margin-top: 20px; padding: 13px 28px; background: #4f46e5; color: white; border-radius: 10px; font-weight: 600; font-size: 15px; text-decoration: none; }
  .btn-go:hover { background: #3730a3; }
  ul { padding-left: 16px; }
  ul li { margin-bottom: 6px; }
  .step { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid #f1f5f9; }
  .step:last-child { border-bottom: none; }
  .check { color: #10b981; }
  .cross { color: #ef4444; }
</style>
</head>
<body>
<div class="card">
  <div class="logo">📚</div>
  <h1>E-Book SMA Installer</h1>
  <p>Memeriksa dan membuat database secara otomatis.</p>

  <?php if (empty($errors)): ?>
    <div class="status ok">✅ Database <strong>ebook_sma</strong> berhasil dibuat!</div>

    <div class="step"><span class="check">✓</span> Tabel <code>users</code> siap</div>
    <div class="step"><span class="check">✓</span> Tabel <code>ebooks</code> siap</div>
    <div class="step"><span class="check">✓</span> Akun admin & user demo ditambahkan</div>

    <div class="status warn">
      ⚠️ <strong>Penting:</strong> Hapus file <code>install.php</code> setelah ini untuk keamanan!
    </div>

    <p style="margin-top:16px"><strong>Login Demo:</strong><br>
      👑 Admin: <code>admin@ebook.com</code> / <code>password</code><br>
      👤 User: <code>user@ebook.com</code> / <code>password</code>
    </p>

    <a href="http://localhost/APIII/login" class="btn-go">🚀 Buka Aplikasi</a>
  <?php else: ?>
    <div class="status err">❌ Terjadi beberapa error:</div>
    <ul>
      <?php foreach ($errors as $e): ?>
        <li><?= htmlspecialchars($e) ?></li>
      <?php endforeach; ?>
    </ul>
    <p style="margin-top:16px">Pastikan XAMPP MySQL aktif dan sesuaikan <code>$pass</code> di file <code>install.php</code>.</p>
  <?php endif; ?>
</div>
</body>
</html>
