<?php
/**
 * ONE-TIME SETUP — creates the database and your first Super Admin account.
 *
 * HOW TO USE:
 *   1. Upload the whole /career folder to your site.
 *   2. Visit: https://yourdomain.com/career/setup.php
 *   3. Fill the form to create your Super Admin login.
 *   4. DELETE THIS FILE (setup.php) immediately after — it will refuse to
 *      run again once a Super Admin exists, but delete it anyway for safety.
 */

require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';

$pdo = get_db();
$existing = $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'superadmin'")->fetchColumn();

$error = '';
$success = false;

if ($existing > 0) {
    $error = 'A Super Admin account already exists. For security this setup page is now locked. Please delete setup.php from the server. If you need a new account, log in and use the "Manage Accounts" section, or ask your developer to reset it.';
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirm = $_POST['confirm'] ?? '';

    if (strlen($name) < 2 || strlen($username) < 3) {
        $error = 'Please provide a valid name and username (min 3 characters).';
    } elseif (strlen($password) < 8) {
        $error = 'Password must be at least 8 characters.';
    } elseif ($password !== $confirm) {
        $error = 'Passwords do not match.';
    } else {
        $stmt = $pdo->prepare('INSERT INTO users (name, username, email, password_hash, role, active) VALUES (?, ?, ?, ?, ?, 1)');
        $stmt->execute([$name, $username, $email, password_hash($password, PASSWORD_DEFAULT), 'superadmin']);
        $success = true;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Career Module Setup — Delmas Apparels</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>body{background:#0e1929;color:#f0ece4;font-family:sans-serif;padding:3rem 1rem}.card{background:#111d30;border:1px solid rgba(201,169,110,.25);border-radius:8px;max-width:480px;margin:0 auto}.form-control{background:#0e1929;border-color:rgba(201,169,110,.25);color:#fff}.form-control:focus{background:#0e1929;color:#fff;border-color:#c9a96e;box-shadow:none}label{color:#8a9ab5;font-size:.85rem}.btn-gold{background:#c9a96e;border:none;color:#1a1e2e;font-weight:600}.btn-gold:hover{background:#e0c896;color:#1a1e2e}</style>
</head>
<body>
<div class="card p-4">
<h4 class="mb-3" style="color:#c9a96e">Career Module — First-Time Setup</h4>
<?php if ($success): ?>
  <div class="alert alert-success">Super Admin account created successfully.</div>
  <p>You can now <a href="admin/login.php" style="color:#e0c896">log in here</a>.</p>
  <p class="text-warning small">⚠️ Please delete <code>career/setup.php</code> from your server now.</p>
<?php else: ?>
  <?php if ($error): ?><div class="alert alert-danger"><?= h($error) ?></div><?php endif; ?>
  <?php if ($existing == 0): ?>
  <form method="post">
    <div class="mb-3"><label>Full Name</label><input class="form-control" name="name" required></div>
    <div class="mb-3"><label>Username</label><input class="form-control" name="username" required></div>
    <div class="mb-3"><label>Email (optional)</label><input class="form-control" type="email" name="email"></div>
    <div class="mb-3"><label>Password</label><input class="form-control" type="password" name="password" minlength="8" required></div>
    <div class="mb-3"><label>Confirm Password</label><input class="form-control" type="password" name="confirm" minlength="8" required></div>
    <button class="btn btn-gold w-100" type="submit">Create Super Admin</button>
  </form>
  <?php endif; ?>
<?php endif; ?>
</div>
<script>document.addEventListener('contextmenu', e => e.preventDefault());</script>
</body>
</html>
