<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $types = [
        [
            'type_code' => 'text',
            'name_ar'   => 'نص',
            'name_en'   => 'Text',
            'renderer_config_json' => '{"input_type":"text","maxlength":500}',
            'validator_rules_json' => '{"base":"string"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'textarea',
            'name_ar'   => 'نص طويل',
            'name_en'   => 'Textarea',
            'renderer_config_json' => '{"rows":4,"maxlength":2000}',
            'validator_rules_json' => '{"base":"string"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'number',
            'name_ar'   => 'رقم',
            'name_en'   => 'Number',
            'renderer_config_json' => '{"input_type":"number","step":"1"}',
            'validator_rules_json' => '{"base":"numeric"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'decimal',
            'name_ar'   => 'رقم عشري',
            'name_en'   => 'Decimal',
            'renderer_config_json' => '{"input_type":"number","step":"0.01"}',
            'validator_rules_json' => '{"base":"numeric"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'date',
            'name_ar'   => 'تاريخ',
            'name_en'   => 'Date',
            'renderer_config_json' => '{"input_type":"date"}',
            'validator_rules_json' => '{"base":"date"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'select',
            'name_ar'   => 'قائمة منسدلة',
            'name_en'   => 'Dropdown',
            'renderer_config_json' => '{"element":"select"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'select_dynamic',
            'name_ar'   => 'قائمة ديناميكية',
            'name_en'   => 'Dynamic Dropdown',
            'renderer_config_json' => '{"element":"select","source":"api"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'national_id',
            'name_ar'   => 'الرقم القومي',
            'name_en'   => 'National ID',
            'renderer_config_json' => '{"input_type":"text","maxlength":14,"pattern":"\\\\d{14}","auto_parse":true}',
            'validator_rules_json' => '{"base":"digits:14|national_id"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'passport',
            'name_ar'   => 'جواز السفر',
            'name_en'   => 'Passport',
            'renderer_config_json' => '{"input_type":"text","maxlength":20}',
            'validator_rules_json' => '{"base":"string|min:5|max:20"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'phone',
            'name_ar'   => 'هاتف',
            'name_en'   => 'Phone',
            'renderer_config_json' => '{"input_type":"tel","maxlength":20}',
            'validator_rules_json' => '{"base":"string|max:20"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'phone_eg',
            'name_ar'   => 'هاتف مصري',
            'name_en'   => 'Egyptian Phone',
            'renderer_config_json' => '{"input_type":"tel","maxlength":11,"pattern":"01[0-9]{9}"}',
            'validator_rules_json' => '{"base":"phone_eg"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'email',
            'name_ar'   => 'بريد إلكتروني',
            'name_en'   => 'Email',
            'renderer_config_json' => '{"input_type":"email"}',
            'validator_rules_json' => '{"base":"email"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'checkbox',
            'name_ar'   => 'اختيار متعدد',
            'name_en'   => 'Checkbox',
            'renderer_config_json' => '{"element":"checkbox"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'radio',
            'name_ar'   => 'اختيار واحد',
            'name_en'   => 'Radio',
            'renderer_config_json' => '{"element":"radio"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'file',
            'name_ar'   => 'ملف',
            'name_en'   => 'File Upload',
            'renderer_config_json' => '{"input_type":"file","accept":"image/*,.pdf"}',
            'validator_rules_json' => '{"base":"file"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'hidden',
            'name_ar'   => 'مخفي',
            'name_en'   => 'Hidden',
            'renderer_config_json' => '{"input_type":"hidden"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'computed',
            'name_ar'   => 'محسوب',
            'name_en'   => 'Computed (Read-only)',
            'renderer_config_json' => '{"readonly":true,"css_class":"computed-field"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'currency',
            'name_ar'   => 'مبلغ مالي',
            'name_en'   => 'Currency',
            'renderer_config_json' => '{"input_type":"number","step":"0.01","suffix":"ج.م","readonly":true}',
            'validator_rules_json' => '{"base":"numeric"}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'auto_increment',
            'name_ar'   => 'رقم تسلسلي',
            'name_en'   => 'Auto Increment',
            'renderer_config_json' => '{"readonly":true}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'signature',
            'name_ar'   => 'توقيع',
            'name_en'   => 'Signature',
            'renderer_config_json' => '{"element":"checkbox","label_override":"أقر بصحة البيانات"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
        [
            'type_code' => 'static_text',
            'name_ar'   => 'نص ثابت',
            'name_en'   => 'Static Text',
            'renderer_config_json' => '{"element":"div","css_class":"static-text"}',
            'validator_rules_json' => '{}',
            'is_system' => 1,
        ],
    ];

    foreach ($types as $type) {
        $existing = $db->selectOne("SELECT id FROM form_field_types WHERE type_code = ?", [$type['type_code']]);
        if ($existing) {
            continue;
        }
        $db->insert('form_field_types', [
            'type_code'            => $type['type_code'],
            'name_ar'              => $type['name_ar'],
            'name_en'              => $type['name_en'],
            'renderer_config_json' => $type['renderer_config_json'],
            'validator_rules_json' => $type['validator_rules_json'],
            'is_system'            => $type['is_system'],
            'is_active'            => 1,
        ]);
    }
};