<?php
declare(strict_types=1);

use App\Core\Registries\MenuRegistry;
use App\Core\Registries\PermissionRegistry;

MenuRegistry::register('forms', [
    'label_ar'   => 'النماذج',
    'label_en'   => 'Forms',
    'icon'       => '📋',
    'route'      => '/forms',
    'permission' => 'forms.view',
    'order'      => 160,
    'children'   => [
        ['label_ar' => 'النماذج المقدمة', 'label_en' => 'Submissions',  'route' => '/forms/submissions', 'permission' => 'forms.view',        'order' => 1],
        ['label_ar' => 'منشئ النماذج',   'label_en' => 'Form Builder', 'route' => '/forms/builder',     'permission' => 'forms.edit_schema', 'order' => 2],
    ],
]);

PermissionRegistry::register('forms', [
    'forms.view'         => ['ar' => 'عرض النماذج',       'en' => 'View Forms'],
    'forms.edit_schema'  => ['ar' => 'تعديل هيكل النماذج', 'en' => 'Edit Form Schemas'],
    'forms.create_schema' => ['ar' => 'إنشاء نموذج',      'en' => 'Create Form Schema'],
]);

try {
    $db = \App\Core\App::getInstance()->db();
    $schemas = $db->select("SELECT form_code, name_ar FROM form_schemas WHERE is_active = 1");
    $formPerms = [];
    foreach ($schemas as $s) {
        $formPerms['forms.access.' . $s['form_code']] = [
            'ar' => 'الوصول لنموذج: ' . $s['name_ar'],
            'en' => 'Access: ' . $s['form_code'],
        ];
    }
    if (!empty($formPerms)) {
        PermissionRegistry::register('forms_access', $formPerms);
    }
} catch (\Throwable $e) {
}