<?php
declare(strict_types=1);

use App\Core\Database;

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

    $schemas = [
        [
            'form_code'  => 'SPORTS_REGISTRATION_MEMBER',
            'name_ar'    => 'تسجيل لاعب عضو في الأنشطة الرياضية',
            'name_en'    => 'Sports Registration - Member',
            'form_fee'   => '50.00',
            'schema_json' => json_encode([
                ['key' => 'member_id',               'type' => 'hidden',       'label_ar' => 'رقم العضو',                              'required' => true,  'auto_populated' => true, 'order' => 1],
                ['key' => 'discipline_ids',           'type' => 'multi_select', 'label_ar' => 'الأنشطة الرياضية المطلوبة',               'required' => true,  'data_source' => 'sport_disciplines', 'order' => 2],
                ['key' => 'academy_id',               'type' => 'select',       'label_ar' => 'الأكاديمية',                              'required' => false, 'data_source' => 'academies', 'order' => 3],
                ['key' => 'level_id',                 'type' => 'select',       'label_ar' => 'المستوى',                                 'required' => false, 'data_source' => 'academy_levels', 'depends_on' => 'academy_id', 'order' => 4],
                ['key' => 'emergency_contact_name',   'type' => 'text',         'label_ar' => 'اسم شخص للتواصل في حالة الطوارئ',         'required' => true,  'validation' => 'required|string|min:5|max:100', 'order' => 5, 'width' => 'half'],
                ['key' => 'emergency_contact_phone',  'type' => 'text',         'label_ar' => 'هاتف الطوارئ',                            'required' => true,  'validation' => 'required|string|max:20', 'order' => 6, 'width' => 'half'],
                ['key' => 'medical_cert_document',    'type' => 'file',         'label_ar' => 'شهادة طبية',                              'required' => false, 'accepted_types' => 'pdf,jpg,png', 'order' => 7, 'width' => 'half'],
                ['key' => 'guardian_consent',          'type' => 'checkbox',     'label_ar' => 'موافقة ولي الأمر',                        'required' => false, 'conditional' => true, 'show_if' => ['field' => 'age', 'operator' => 'lt', 'value' => 18], 'order' => 8, 'width' => 'half'],
                ['key' => 'notes',                    'type' => 'textarea',     'label_ar' => 'ملاحظات',                                 'required' => false, 'order' => 9, 'width' => 'full'],
            ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
        ],
        [
            'form_code'  => 'SPORTS_REGISTRATION_NONMEMBER',
            'name_ar'    => 'تسجيل لاعب غير عضو في الأنشطة الرياضية',
            'name_en'    => 'Sports Registration - Non-Member',
            'form_fee'   => '100.00',
            'schema_json' => json_encode([
                ['key' => 'full_name_ar',          'type' => 'text',         'label_ar' => 'الاسم الكامل بالعربي',                 'required' => true,  'validation' => 'required|string|min:10|max:200', 'order' => 1, 'width' => 'full'],
                ['key' => 'full_name_en',          'type' => 'text',         'label_ar' => 'الاسم بالإنجليزي',                     'required' => false, 'validation' => 'nullable|string|max:200', 'order' => 2, 'width' => 'half'],
                ['key' => 'national_id',           'type' => 'text',         'label_ar' => 'الرقم القومي',                         'required' => true,  'validation' => 'required|digits:14', 'order' => 3, 'width' => 'half'],
                ['key' => 'date_of_birth',         'type' => 'date',         'label_ar' => 'تاريخ الميلاد',                        'required' => true,  'validation' => 'required|date', 'order' => 4, 'width' => 'half'],
                ['key' => 'gender',                'type' => 'select',       'label_ar' => 'النوع',                                'required' => true,  'options' => [['value' => 'male', 'label_ar' => 'ذكر'], ['value' => 'female', 'label_ar' => 'أنثى']], 'order' => 5, 'width' => 'half'],
                ['key' => 'phone',                 'type' => 'text',         'label_ar' => 'الهاتف',                               'required' => true,  'validation' => 'required|string|max:20', 'order' => 6, 'width' => 'half'],
                ['key' => 'guardian_name',          'type' => 'text',         'label_ar' => 'اسم ولي الأمر',                       'required' => false, 'conditional' => true, 'show_if' => ['field' => 'age', 'operator' => 'lt', 'value' => 18], 'order' => 7, 'width' => 'half'],
                ['key' => 'guardian_phone',         'type' => 'text',         'label_ar' => 'هاتف ولي الأمر',                      'required' => false, 'conditional' => true, 'show_if' => ['field' => 'age', 'operator' => 'lt', 'value' => 18], 'order' => 8, 'width' => 'half'],
                ['key' => 'guardian_national_id',   'type' => 'text',         'label_ar' => 'الرقم القومي لولي الأمر',             'required' => false, 'conditional' => true, 'show_if' => ['field' => 'age', 'operator' => 'lt', 'value' => 18], 'order' => 9, 'width' => 'half'],
                ['key' => 'guardian_relationship',  'type' => 'select',       'label_ar' => 'صلة القرابة',                         'required' => false, 'conditional' => true, 'show_if' => ['field' => 'age', 'operator' => 'lt', 'value' => 18], 'options' => [['value' => 'father', 'label_ar' => 'الأب'], ['value' => 'mother', 'label_ar' => 'الأم'], ['value' => 'brother', 'label_ar' => 'الأخ'], ['value' => 'sister', 'label_ar' => 'الأخت'], ['value' => 'other', 'label_ar' => 'أخرى']], 'order' => 10, 'width' => 'half'],
                ['key' => 'discipline_ids',         'type' => 'multi_select', 'label_ar' => 'الأنشطة الرياضية المطلوبة',            'required' => true,  'data_source' => 'sport_disciplines', 'order' => 11],
                ['key' => 'academy_id',             'type' => 'select',       'label_ar' => 'الأكاديمية',                           'required' => false, 'data_source' => 'academies', 'order' => 12],
                ['key' => 'medical_cert_document',  'type' => 'file',         'label_ar' => 'شهادة طبية',                           'required' => false, 'accepted_types' => 'pdf,jpg,png', 'order' => 13, 'width' => 'half'],
                ['key' => 'photo',                  'type' => 'file',         'label_ar' => 'صورة شخصية',                           'required' => false, 'accepted_types' => 'jpg,png', 'order' => 14, 'width' => 'half'],
                ['key' => 'notes',                  'type' => 'textarea',     'label_ar' => 'ملاحظات',                              'required' => false, 'order' => 15, 'width' => 'full'],
            ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
        ],
    ];

    foreach ($schemas as $s) {
        $existing = $db->selectOne("SELECT id FROM form_schemas WHERE form_code = ?", [$s['form_code']]);
        if ($existing) {
            continue;
        }
        $db->insert('form_schemas', [
            'form_code'   => $s['form_code'],
            'name_ar'     => $s['name_ar'],
            'name_en'     => $s['name_en'],
            'form_fee'    => $s['form_fee'],
            'schema_json' => $s['schema_json'],
            'is_active'   => 1,
            'version'     => 1,
            'created_at'  => $ts,
            'updated_at'  => $ts,
        ]);
    }
};
