<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $existing = $db->selectOne("SELECT id FROM employees WHERE username = ?", ['admin']);
    if ($existing) {
        return;
    }

    $passwordHash = password_hash('ClubManagement', PASSWORD_BCRYPT, ['cost' => 12]);

    $employeeId = $db->insert('employees', [
        'username'              => 'admin',
        'password_hash'         => $passwordHash,
        'full_name_ar'          => 'مدير النظام',
        'full_name_en'          => 'System Administrator',
        'email'                 => 'admin@theclub.com',
        'is_active'             => 1,
        'force_password_change' => 0,
        'created_at'            => date('Y-m-d H:i:s'),
        'updated_at'            => date('Y-m-d H:i:s'),
    ]);

    $superRole = $db->selectOne("SELECT id FROM roles WHERE role_code = ?", ['super_admin']);
    if ($superRole) {
        $db->insert('employee_roles', [
            'employee_id' => $employeeId,
            'role_id'     => (int) $superRole['id'],
            'assigned_at' => date('Y-m-d H:i:s'),
            'is_active'   => 1,
        ]);
    }

    $db->insert('password_history', [
        'employee_id'   => $employeeId,
        'password_hash' => $passwordHash,
        'created_at'    => date('Y-m-d H:i:s'),
    ]);
};