<?php
declare(strict_types=1);

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

    $permissionKeys = [
        'treasury.view_dashboard',
        'treasury.collect_payment',
        'treasury.open_session',
        'treasury.close_session',
        'treasury.initiate_settlement',
        'treasury.view_custody',
        'treasury.view_sessions',
        'treasury.view_settlements',
        'cashier.receive_settlement',
        'cashier.manage_deposits',
        'cashier.confirm_deposit',
        'cashier.view_custody',
    ];

    // Assign all treasury/cashier permissions to super_admin role
    $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,
            ]);
        }
    }
};
