<?php
declare(strict_types=1);

return function (\App\Core\Database $db): void {
    $now = date('Y-m-d H:i:s');

    $permissionKeys = [
        'sa.locker.view',
        'sa.locker.manage',
        'sa.locker_rental.view',
        'sa.locker_rental.create',
        'sa.locker_rental.manage',
        'sa.locker_rental.evict',
        'sales.referral.view',
    ];

    $superAdmin = $db->selectOne("SELECT id FROM roles WHERE role_code = 'super_admin'");
    if (!$superAdmin) {
        return;
    }

    $roleId = (int) $superAdmin['id'];

    foreach ($permissionKeys as $key) {
        $exists = $db->selectOne(
            "SELECT 1 FROM role_permissions WHERE role_id = ? AND permission_key = ?",
            [$roleId, $key]
        );
        if (!$exists) {
            $db->insert('role_permissions', [
                'role_id'        => $roleId,
                'permission_key' => $key,
                'granted_at'     => $now,
            ]);
        }
    }
};
