<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $tableExists = $db->selectOne(
        "SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'permissions'"
    );
    if (!$tableExists) {
        return;
    }

    $permissions = [
        ['key' => 'support.view',        'name_ar' => 'عرض تذاكر الدعم',    'name_en' => 'View Support Tickets',    'module' => 'support'],
        ['key' => 'support.create',      'name_ar' => 'إنشاء تذكرة دعم',    'name_en' => 'Create Support Ticket',   'module' => 'support'],
        ['key' => 'support.reply',       'name_ar' => 'الرد على التذاكر',    'name_en' => 'Reply to Tickets',        'module' => 'support'],
        ['key' => 'support.assign',      'name_ar' => 'تعيين التذاكر',       'name_en' => 'Assign Tickets',          'module' => 'support'],
        ['key' => 'support.close',       'name_ar' => 'إغلاق التذاكر',      'name_en' => 'Close Tickets',           'module' => 'support'],
        ['key' => 'support.manage',      'name_ar' => 'إدارة تذاكر الدعم',  'name_en' => 'Manage Support Tickets',  'module' => 'support'],
    ];

    foreach ($permissions as $p) {
        $exists = $db->selectOne("SELECT id FROM permissions WHERE `key` = ?", [$p['key']]);
        if (!$exists) {
            $db->insert('permissions', [
                'key'      => $p['key'],
                'name_ar'  => $p['name_ar'],
                'name_en'  => $p['name_en'],
                'module'   => $p['module'],
            ]);
        }
    }
};
