<?php
/**
 * ANATL Portal Setup Script
 * Run this script once to setup the database
 */

require_once 'includes/config.php';

// Create database connection without database selection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, null, DB_PORT);

if ($conn->connect_error) {
    die('Connection failed: ' . htmlspecialchars($conn->connect_error));
}

// Create database
$create_db = "CREATE DATABASE IF NOT EXISTS " . DB_NAME . " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
$conn->query($create_db);

// Select database
$conn->select_db(DB_NAME);

// Read and execute setup SQL
$setup_sql = file_get_contents(__DIR__ . '/db_setup.sql');
$statements = explode(';', $setup_sql);

foreach ($statements as $statement) {
    $statement = trim($statement);
    if (!empty($statement)) {
        if (!$conn->query($statement)) {
            echo "Error executing statement: " . htmlspecialchars($conn->error) . "<br>";
        }
    }
}

$conn->close();

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ANATL Setup</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; }
        .success { color: #059669; padding: 20px; background: #D1FAE5; border-radius: 8px; }
    </style>
</head>
<body>
<div class="success">
    <h2>✅ Setup Berhasil!</h2>
    <p>Database ANATL Portal telah berhasil dibuat dan dikonfigurasi.</p>
    <p>Anda dapat sekarang <a href="/">login ke portal</a></p>
    <hr>
    <h3>Default Login Credentials:</h3>
    <ul>
        <li><strong>Email:</strong> admin@anatl.tl</li>
        <li><strong>Password:</strong> password</li>
        <li><strong>Role:</strong> admin</li>
    </ul>
</div>
</body>
</html>
