<?php
session_start();
require_once "config/database.php";

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header("Location: login.php");
    exit;
}

$database = new Database();
$db = $database->getConnection();

if (!$db) {
    die("Database connection failed.");
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $full_name = trim($_POST['full_name'] ?? '');
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $role = $_POST['role'] ?? 'user';
    $status = $_POST['status'] ?? 'active';

    if ($full_name === '' || $username === '' || $email === '' || $password === '') {
        $error = 'Please fill in all required fields.';
    } else {
        try {
            $check = $db->prepare("SELECT id FROM users WHERE username = :username OR email = :email LIMIT 1");
            $check->execute([
                ':username' => $username,
                ':email' => $email
            ]);

            if ($check->fetch()) {
                $error = 'Username or email already exists.';
            } else {
                $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

                $stmt = $db->prepare("
                    INSERT INTO users (full_name, username, email, password, role, status, created_at)
                    VALUES (:full_name, :username, :email, :password, :role, :status, NOW())
                ");

                $stmt->execute([
                    ':full_name' => $full_name,
                    ':username' => $username,
                    ':email' => $email,
                    ':password' => $hashedPassword,
                    ':role' => $role,
                    ':status' => $status
                ]);

                header("Location: users.php?created=1");
                exit;
            }
        } catch (PDOException $e) {
            $error = "Error: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add User</title>
    <link rel="stylesheet" href="assets/css/app.css">
</head>
<body>
<div class="layout">
    <?php include 'nav.php'; ?>

    <main class="main">
        <div class="hero">
            <div class="hero-top">
                <div>
                    <h1>Add New User</h1>
                    <p>Create a user account manually as admin.</p>
                </div>
                <a href="users.php" class="btn-primary">Back to Users</a>
            </div>
        </div>

        <div class="card">
            <?php if ($error): ?>
                <div class="badge danger" style="margin-bottom:12px;">
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>

            <form method="POST">
                <div class="form-group">
                    <label>Full Name</label>
                    <input type="text" name="full_name" required>
                </div>

                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" required>
                </div>

                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" required>
                </div>

                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" required>
                </div>

                <div class="form-group">
                    <label>Role</label>
                    <select name="role">
                        <option value="user">User</option>
                        <option value="admin">Admin</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Status</label>
                    <select name="status">
                        <option value="active">Active</option>
                        <option value="inactive">Inactive</option>
                    </select>
                </div>

                <button type="submit" class="btn-primary">Create User</button>
            </form>
        </div>
    </main>
</div>
</body>
</html>