<?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'     => 'NEW_MEMBERSHIP',
            'name_ar'       => 'استمارة عضوية جديدة',
            'name_en'       => 'New Membership Application',
            'form_fee'      => '505.00',
            'validity_days' => 15,
            'schema_json'   => json_encode([
                'sections' => [
                    [
                        'key' => 'personal_data', 'label_ar' => 'البيانات الشخصية', 'order' => 1, 'visible_when' => null,
                        'fields' => [
                            ['key' => 'full_name_ar', 'type' => 'text', 'label_ar' => 'الاسم بالكامل (عربي)', 'required' => true, 'editable' => true, 'validation' => 'required|string|min:10|max:200', 'order' => 1, 'width' => 'full'],
                            ['key' => 'full_name_en', 'type' => 'text', 'label_ar' => 'الاسم بالكامل (إنجليزي)', 'required' => false, 'editable' => true, 'validation' => 'nullable|string|max:200', 'order' => 2, 'width' => 'half'],
                            ['key' => 'id_type', 'type' => 'select', 'label_ar' => 'نوع إثبات الشخصية', 'required' => true, 'editable' => true, 'validation' => 'required|in:national_id,passport,military_id', 'order' => 3, 'width' => 'half', 'options' => [['value' => 'national_id', 'label_ar' => 'رقم قومي'], ['value' => 'passport', 'label_ar' => 'جواز سفر'], ['value' => 'military_id', 'label_ar' => 'بطاقة عسكرية']]],
                            ['key' => 'national_id', 'type' => 'national_id', 'label_ar' => 'الرقم القومي', 'required' => true, 'editable' => true, 'auto_parse' => true, 'populates' => ['date_of_birth', 'age_years', 'age_months', 'gender', 'governorate_code'], 'validation' => 'required|digits:14|national_id', 'order' => 4, 'width' => 'half', 'visible_when' => ['field' => 'id_type', 'operator' => 'eq', 'value' => 'national_id'], 'help_text_ar' => 'أدخل الرقم القومي المكون من 14 رقم'],
                            ['key' => 'passport_number', 'type' => 'passport', 'label_ar' => 'رقم جواز السفر', 'required' => true, 'editable' => true, 'validation' => 'required|string|min:5|max:20', 'order' => 5, 'width' => 'half', 'visible_when' => ['field' => 'id_type', 'operator' => 'eq', 'value' => 'passport']],
                            ['key' => 'date_of_birth', 'type' => 'date', 'label_ar' => 'تاريخ الميلاد', 'required' => true, 'editable' => false, 'validation' => 'required|date', 'order' => 6, 'width' => 'quarter', 'computed_from' => 'national_id'],
                            ['key' => 'age_years', 'type' => 'computed', 'label_ar' => 'السن (سنوات)', 'required' => false, 'editable' => false, 'order' => 7, 'width' => 'quarter'],
                            ['key' => 'age_months', 'type' => 'computed', 'label_ar' => 'السن (أشهر)', 'required' => false, 'editable' => false, 'order' => 8, 'width' => 'quarter'],
                            ['key' => 'gender', 'type' => 'select', 'label_ar' => 'النوع', 'required' => true, 'editable' => false, 'validation' => 'required|in:male,female', 'order' => 9, 'width' => 'quarter', 'options' => [['value' => 'male', 'label_ar' => 'ذكر'], ['value' => 'female', 'label_ar' => 'أنثى']], 'computed_from' => 'national_id'],
                            ['key' => 'governorate_code', 'type' => 'select_dynamic', 'label_ar' => 'محافظة الميلاد', 'required' => false, 'editable' => false, 'order' => 10, 'width' => 'half', 'data_source' => 'governorates', 'computed_from' => 'national_id'],
                            ['key' => 'nationality', 'type' => 'select_dynamic', 'label_ar' => 'الجنسية', 'required' => true, 'editable' => true, 'validation' => 'required|string', 'order' => 11, 'width' => 'half', 'data_source' => 'countries', 'default_value' => 'مصري'],
                            ['key' => 'religion', 'type' => 'select', 'label_ar' => 'الديانة', 'required' => true, 'editable' => true, 'validation' => 'required|string', 'order' => 12, 'width' => 'half', 'options' => [['value' => 'muslim', 'label_ar' => 'مسلم'], ['value' => 'christian', 'label_ar' => 'مسيحي'], ['value' => 'other', 'label_ar' => 'أخرى']]],
                            ['key' => 'qualification_id', 'type' => 'select_dynamic', 'label_ar' => 'المؤهل الدراسي', 'required' => true, 'editable' => true, 'validation' => 'required|integer', 'order' => 13, 'width' => 'half', 'data_source' => 'qualifications', 'help_text_ar' => 'المؤهل يؤثر على قيمة العضوية'],
                            ['key' => 'marital_status', 'type' => 'select', 'label_ar' => 'الحالة الاجتماعية', 'required' => true, 'editable' => true, 'validation' => 'required|in:single,married,divorced,widowed', 'order' => 14, 'width' => 'half', 'options' => [['value' => 'single', 'label_ar' => 'أعزب'], ['value' => 'married', 'label_ar' => 'متزوج'], ['value' => 'divorced', 'label_ar' => 'مطلق'], ['value' => 'widowed', 'label_ar' => 'أرمل']]],
                            ['key' => 'id_issue_date', 'type' => 'date', 'label_ar' => 'تاريخ إصدار إثبات الشخصية', 'required' => true, 'editable' => true, 'validation' => 'required|date|date_before_today', 'order' => 15, 'width' => 'half'],
                            ['key' => 'id_expiry_date', 'type' => 'date', 'label_ar' => 'تاريخ انتهاء إثبات الشخصية', 'required' => false, 'editable' => true, 'validation' => 'nullable|date', 'order' => 16, 'width' => 'half'],
                        ],
                    ],
                    [
                        'key' => 'contact', 'label_ar' => 'بيانات الاتصال', 'order' => 2, 'visible_when' => null,
                        'fields' => [
                            ['key' => 'phone_mobile', 'type' => 'phone_eg', 'label_ar' => 'رقم المحمول', 'required' => true, 'editable' => true, 'validation' => 'required|phone_eg', 'order' => 1, 'width' => 'half'],
                            ['key' => 'phone_home', 'type' => 'phone', 'label_ar' => 'تليفون المنزل', 'required' => false, 'editable' => true, 'validation' => 'nullable|string|max:20', 'order' => 2, 'width' => 'half'],
                            ['key' => 'phone_international', 'type' => 'phone', 'label_ar' => 'رقم تليفون خارج مصر', 'required' => false, 'editable' => true, 'validation' => 'nullable|string|max:30', 'order' => 3, 'width' => 'half'],
                            ['key' => 'email', 'type' => 'email', 'label_ar' => 'البريد الإلكتروني', 'required' => false, 'editable' => true, 'validation' => 'nullable|email', 'order' => 4, 'width' => 'half'],
                            ['key' => 'emergency_name', 'type' => 'text', 'label_ar' => 'شخص للطوارئ — الاسم', 'required' => true, 'editable' => true, 'validation' => 'required|string|min:5|max:100', 'order' => 5, 'width' => 'half'],
                            ['key' => 'emergency_phone', 'type' => 'phone_eg', 'label_ar' => 'شخص للطوارئ — المحمول', 'required' => true, 'editable' => true, 'validation' => 'required|phone_eg', 'order' => 6, 'width' => 'half'],
                        ],
                    ],
                    [
                        'key' => 'residence', 'label_ar' => 'بيانات السكن', 'order' => 3, 'visible_when' => null,
                        'fields' => [
                            ['key' => 'residence_type', 'type' => 'select', 'label_ar' => 'نوع السكن', 'required' => true, 'editable' => true, 'validation' => 'required|in:rented,owned,other', 'order' => 1, 'width' => 'half', 'options' => [['value' => 'rented', 'label_ar' => 'إيجار'], ['value' => 'owned', 'label_ar' => 'ملك'], ['value' => 'other', 'label_ar' => 'أخرى']]],
                            ['key' => 'residence_address', 'type' => 'textarea', 'label_ar' => 'عنوان السكن', 'required' => true, 'editable' => true, 'validation' => 'required|string|min:10|max:500', 'order' => 2, 'width' => 'full'],
                            ['key' => 'landmark', 'type' => 'text', 'label_ar' => 'علامة مميزة', 'required' => false, 'editable' => true, 'order' => 3, 'width' => 'half'],
                            ['key' => 'floor', 'type' => 'text', 'label_ar' => 'الدور', 'required' => false, 'editable' => true, 'order' => 4, 'width' => 'quarter'],
                            ['key' => 'apartment', 'type' => 'text', 'label_ar' => 'رقم الشقة', 'required' => false, 'editable' => true, 'order' => 5, 'width' => 'quarter'],
                            ['key' => 'area', 'type' => 'text', 'label_ar' => 'المنطقة', 'required' => true, 'editable' => true, 'validation' => 'required|string', 'order' => 6, 'width' => 'half'],
                            ['key' => 'governorate', 'type' => 'select_dynamic', 'label_ar' => 'المحافظة', 'required' => true, 'editable' => true, 'validation' => 'required', 'order' => 7, 'width' => 'half', 'data_source' => 'governorates'],
                            ['key' => 'correspondence_address', 'type' => 'select', 'label_ar' => 'عنوان المراسلات', 'required' => true, 'editable' => true, 'validation' => 'required|in:work,residence,other', 'order' => 8, 'width' => 'half', 'options' => [['value' => 'work', 'label_ar' => 'العمل'], ['value' => 'residence', 'label_ar' => 'السكن'], ['value' => 'other', 'label_ar' => 'أخرى']]],
                        ],
                    ],
                    [
                        'key' => 'work', 'label_ar' => 'بيانات العمل', 'order' => 4, 'visible_when' => null,
                        'fields' => [
                            ['key' => 'employment_type', 'type' => 'select', 'label_ar' => 'نوع التوظيف', 'required' => true, 'editable' => true, 'validation' => 'required|in:employed,self_employed,professions,other', 'order' => 1, 'width' => 'half', 'options' => [['value' => 'employed', 'label_ar' => 'موظف'], ['value' => 'self_employed', 'label_ar' => 'أعمال حرة'], ['value' => 'professions', 'label_ar' => 'مهن حرة'], ['value' => 'other', 'label_ar' => 'أخرى']]],
                            ['key' => 'occupation', 'type' => 'text', 'label_ar' => 'المهنة', 'required' => true, 'editable' => true, 'validation' => 'required|string', 'order' => 2, 'width' => 'half'],
                            ['key' => 'job_title', 'type' => 'text', 'label_ar' => 'المركز الوظيفي', 'required' => false, 'editable' => true, 'order' => 3, 'width' => 'half'],
                            ['key' => 'employment_date', 'type' => 'date', 'label_ar' => 'تاريخ الالتحاق بالعمل', 'required' => false, 'editable' => true, 'validation' => 'nullable|date', 'order' => 4, 'width' => 'half'],
                            ['key' => 'business_address', 'type' => 'textarea', 'label_ar' => 'عنوان العمل', 'required' => false, 'editable' => true, 'order' => 5, 'width' => 'full'],
                            ['key' => 'office_phone', 'type' => 'phone', 'label_ar' => 'تليفون العمل', 'required' => false, 'editable' => true, 'order' => 6, 'width' => 'half'],
                            ['key' => 'office_fax', 'type' => 'phone', 'label_ar' => 'فاكس العمل', 'required' => false, 'editable' => true, 'order' => 7, 'width' => 'half'],
                            ['key' => 'business_activity', 'type' => 'text', 'label_ar' => 'نشاط العمل', 'required' => false, 'editable' => true, 'order' => 8, 'width' => 'half'],
                        ],
                    ],
                    [
                        'key' => 'referral', 'label_ar' => 'كيف عرفت النادي', 'order' => 5, 'visible_when' => null,
                        'fields' => [
                            ['key' => 'referral_social_media', 'type' => 'checkbox', 'label_ar' => 'مواقع التواصل الاجتماعي', 'required' => false, 'order' => 1, 'width' => 'half'],
                            ['key' => 'referral_tv', 'type' => 'checkbox', 'label_ar' => 'إعلان التليفزيون', 'required' => false, 'order' => 2, 'width' => 'half'],
                            ['key' => 'referral_friend', 'type' => 'checkbox', 'label_ar' => 'من خلال صديق', 'required' => false, 'order' => 3, 'width' => 'half'],
                            ['key' => 'referral_radio', 'type' => 'checkbox', 'label_ar' => 'إعلان الراديو', 'required' => false, 'order' => 4, 'width' => 'half'],
                            ['key' => 'referral_outdoor', 'type' => 'checkbox', 'label_ar' => 'إعلانات الطريق', 'required' => false, 'order' => 5, 'width' => 'half'],
                        ],
                    ],
                    [
                        'key' => 'notes_section', 'label_ar' => 'ملاحظات', 'order' => 6, 'visible_when' => null,
                        'fields' => [
                            ['key' => 'notes', 'type' => 'textarea', 'label_ar' => 'ملاحظات', 'required' => false, 'editable' => true, 'order' => 1, 'width' => 'full'],
                        ],
                    ],
                ],
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'form_code'     => 'TRANSFER_SEPARATION',
            'name_ar'       => 'استمارة تحويل / فصل',
            'name_en'       => 'Transfer / Separation Form',
            'form_fee'      => '570.00',
            'validity_days' => 15,
            'schema_json'   => json_encode([
                'sections' => [
                    [
                        'key' => 'transfer_info', 'label_ar' => 'بيانات التحويل', 'order' => 1,
                        'fields' => [
                            ['key' => 'transfer_type', 'type' => 'select', 'label_ar' => 'نوع التحويل', 'required' => true, 'validation' => 'required|in:divorce,death,child_separation,child_mandatory_25,waiver,sports_conversion,cross_branch', 'order' => 1, 'width' => 'half', 'options' => [['value' => 'divorce', 'label_ar' => 'طلاق'], ['value' => 'death', 'label_ar' => 'وفاة'], ['value' => 'child_separation', 'label_ar' => 'فصل أبناء'], ['value' => 'child_mandatory_25', 'label_ar' => 'تحويل وجوبي 25'], ['value' => 'waiver', 'label_ar' => 'تنازل'], ['value' => 'sports_conversion', 'label_ar' => 'تحويل رياضي'], ['value' => 'cross_branch', 'label_ar' => 'تحويل بين فروع']]],
                            ['key' => 'source_membership_number', 'type' => 'text', 'label_ar' => 'رقم العضوية المصدر', 'required' => true, 'editable' => false, 'order' => 2, 'width' => 'half'],
                            ['key' => 'original_acquisition_date', 'type' => 'date', 'label_ar' => 'تاريخ اكتساب العضوية الأصلية', 'required' => true, 'editable' => false, 'order' => 3, 'width' => 'half'],
                            ['key' => 'years_since_acquisition', 'type' => 'computed', 'label_ar' => 'عدد السنوات منذ الاكتساب', 'required' => false, 'editable' => false, 'order' => 4, 'width' => 'quarter'],
                            ['key' => 'new_membership_value', 'type' => 'currency', 'label_ar' => 'قيمة العضوية الجديدة وقت الطلب', 'required' => true, 'editable' => false, 'order' => 5, 'width' => 'half'],
                            ['key' => 'fee_percentage', 'type' => 'computed', 'label_ar' => 'نسبة رسوم الفصل', 'required' => false, 'editable' => false, 'order' => 6, 'width' => 'quarter'],
                            ['key' => 'separation_fee', 'type' => 'currency', 'label_ar' => 'مبلغ رسوم الفصل', 'required' => false, 'editable' => false, 'order' => 7, 'width' => 'half'],
                            ['key' => 'form_fee_display', 'type' => 'currency', 'label_ar' => 'رسوم الاستمارة', 'required' => false, 'editable' => false, 'order' => 8, 'width' => 'quarter', 'default_value' => '570.00'],
                            ['key' => 'annual_subscription_fee', 'type' => 'currency', 'label_ar' => 'الاشتراك السنوي', 'required' => false, 'editable' => false, 'order' => 9, 'width' => 'quarter'],
                            ['key' => 'total_fee', 'type' => 'currency', 'label_ar' => 'الإجمالي المطلوب', 'required' => false, 'editable' => false, 'order' => 10, 'width' => 'half'],
                            ['key' => 'fee_receipt_number', 'type' => 'text', 'label_ar' => 'رقم إيصال الخزينة', 'required' => true, 'editable' => true, 'validation' => 'required|string', 'order' => 11, 'width' => 'half'],
                        ],
                    ],
                    [
                        'key' => 'personal_data', 'label_ar' => 'البيانات الشخصية للعضو الجديد', 'order' => 2,
                        'fields' => [
                            ['key' => 'full_name_ar', 'type' => 'text', 'label_ar' => 'الاسم بالكامل (عربي)', 'required' => true, 'validation' => 'required|string|min:10|max:200', 'order' => 1, 'width' => 'full'],
                            ['key' => 'national_id', 'type' => 'national_id', 'label_ar' => 'الرقم القومي', 'required' => true, 'auto_parse' => true, 'populates' => ['date_of_birth', 'age_years', 'gender', 'governorate_code'], 'validation' => 'required|digits:14|national_id', 'order' => 2, 'width' => 'half'],
                            ['key' => 'date_of_birth', 'type' => 'date', 'label_ar' => 'تاريخ الميلاد', 'required' => true, 'editable' => false, 'order' => 3, 'width' => 'quarter'],
                            ['key' => 'age_years', 'type' => 'computed', 'label_ar' => 'السن', 'editable' => false, 'order' => 4, 'width' => 'quarter'],
                            ['key' => 'gender', 'type' => 'select', 'label_ar' => 'النوع', 'editable' => false, 'order' => 5, 'width' => 'quarter', 'options' => [['value' => 'male', 'label_ar' => 'ذكر'], ['value' => 'female', 'label_ar' => 'أنثى']]],
                            ['key' => 'qualification_id', 'type' => 'select_dynamic', 'label_ar' => 'المؤهل', 'required' => true, 'validation' => 'required|integer', 'order' => 6, 'width' => 'half', 'data_source' => 'qualifications'],
                            ['key' => 'phone_mobile', 'type' => 'phone_eg', 'label_ar' => 'رقم المحمول', 'required' => true, 'validation' => 'required|phone_eg', 'order' => 7, 'width' => 'half'],
                        ],
                    ],
                    [
                        'key' => 'notes_section', 'label_ar' => 'ملاحظات', 'order' => 3,
                        'fields' => [
                            ['key' => 'notes', 'type' => 'textarea', 'label_ar' => 'ملاحظات', 'required' => false, 'order' => 1, 'width' => 'full'],
                        ],
                    ],
                ],
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'form_code'     => 'ADDITION_CHILD',
            'name_ar'       => 'استمارة ضم أبناء',
            'name_en'       => 'Children Addition Form',
            'form_fee'      => '570.00',
            'validity_days' => 15,
            'schema_json'   => json_encode([
                'sections' => [
                    [
                        'key' => 'owner_info', 'label_ar' => 'بيانات صاحب العضوية', 'order' => 1,
                        'fields' => [
                            ['key' => 'member_name', 'type' => 'text', 'label_ar' => 'اسم مقدم الطلب', 'required' => true, 'editable' => false, 'order' => 1, 'width' => 'half'],
                            ['key' => 'membership_number', 'type' => 'text', 'label_ar' => 'رقم العضوية', 'required' => true, 'editable' => false, 'order' => 2, 'width' => 'quarter'],
                            ['key' => 'member_phone', 'type' => 'phone_eg', 'label_ar' => 'رقم المحمول', 'required' => true, 'editable' => false, 'order' => 3, 'width' => 'quarter'],
                        ],
                    ],
                    [
                        'key' => 'children', 'label_ar' => 'بيانات الأبناء المراد ضمهم', 'order' => 2, 'repeatable' => true, 'max_repeats' => 3,
                        'fields' => [
                            ['key' => 'child_name', 'type' => 'text', 'label_ar' => 'الاسم', 'required' => true, 'validation' => 'required|string|min:5|max:200', 'order' => 1, 'width' => 'half'],
                            ['key' => 'child_relationship', 'type' => 'select', 'label_ar' => 'الصفة', 'required' => true, 'validation' => 'required|in:son,daughter,temporary', 'order' => 2, 'width' => 'quarter', 'options' => [['value' => 'son', 'label_ar' => 'ابن'], ['value' => 'daughter', 'label_ar' => 'ابنة'], ['value' => 'temporary', 'label_ar' => 'مؤقت']]],
                            ['key' => 'child_gender', 'type' => 'select', 'label_ar' => 'النوع', 'required' => true, 'order' => 3, 'width' => 'quarter', 'options' => [['value' => 'male', 'label_ar' => 'ذكر'], ['value' => 'female', 'label_ar' => 'أنثى']]],
                            ['key' => 'child_national_id', 'type' => 'national_id', 'label_ar' => 'الرقم القومي', 'required' => false, 'auto_parse' => true, 'populates' => ['child_dob', 'child_age', 'child_gender'], 'order' => 4, 'width' => 'half', 'help_text_ar' => 'مطلوب للأبناء فوق 16 سنة'],
                            ['key' => 'child_dob', 'type' => 'date', 'label_ar' => 'تاريخ الميلاد', 'required' => true, 'order' => 5, 'width' => 'quarter'],
                            ['key' => 'child_age', 'type' => 'computed', 'label_ar' => 'السن', 'editable' => false, 'order' => 6, 'width' => 'quarter'],
                            ['key' => 'child_temp_category', 'type' => 'select', 'label_ar' => 'فئة العضو المؤقت', 'required' => false, 'order' => 7, 'width' => 'half', 'visible_when' => ['field' => 'child_relationship', 'operator' => 'eq', 'value' => 'temporary'], 'options' => [['value' => 'parent', 'label_ar' => 'والدين'], ['value' => 'special_needs', 'label_ar' => 'ذوي احتياجات خاصة'], ['value' => 'unmarried_daughter', 'label_ar' => 'بنات غير متزوجات'], ['value' => 'sister', 'label_ar' => 'شقيقة'], ['value' => 'stepchild', 'label_ar' => 'أبناء الزوج/الزوجة'], ['value' => 'orphan', 'label_ar' => 'طفل يتيم'], ['value' => 'disabled_sibling', 'label_ar' => 'شقيق معاق'], ['value' => 'nanny', 'label_ar' => 'مربية']]],
                            ['key' => 'child_has_championship', 'type' => 'checkbox', 'label_ar' => 'حاصل على بطولات جمهورية', 'required' => false, 'order' => 8, 'width' => 'half'],
                            ['key' => 'child_fee', 'type' => 'currency', 'label_ar' => 'رسوم الإضافة', 'editable' => false, 'order' => 9, 'width' => 'half'],
                            ['key' => 'child_notes', 'type' => 'textarea', 'label_ar' => 'ملاحظات', 'required' => false, 'order' => 10, 'width' => 'full'],
                        ],
                    ],
                ],
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'form_code'     => 'ADDITION_SPOUSE',
            'name_ar'       => 'استمارة ضم زوج / زوجة',
            'name_en'       => 'Spouse Addition Form',
            'form_fee'      => '570.00',
            'validity_days' => 15,
            'schema_json'   => json_encode([
                'sections' => [
                    [
                        'key' => 'spouse_data', 'label_ar' => 'بيانات الزوج/الزوجة', 'order' => 1,
                        'fields' => [
                            ['key' => 'spouse_name', 'type' => 'text', 'label_ar' => 'الاسم', 'required' => true, 'validation' => 'required|string|min:10|max:200', 'order' => 1, 'width' => 'full'],
                            ['key' => 'spouse_national_id', 'type' => 'national_id', 'label_ar' => 'الرقم القومي', 'required' => true, 'auto_parse' => true, 'populates' => ['spouse_dob', 'spouse_age', 'spouse_gender'], 'validation' => 'required|digits:14|national_id', 'order' => 2, 'width' => 'half'],
                            ['key' => 'spouse_dob', 'type' => 'date', 'label_ar' => 'تاريخ الميلاد', 'editable' => false, 'order' => 3, 'width' => 'quarter'],
                            ['key' => 'spouse_age', 'type' => 'computed', 'label_ar' => 'السن', 'editable' => false, 'order' => 4, 'width' => 'quarter'],
                            ['key' => 'spouse_gender', 'type' => 'select', 'label_ar' => 'النوع', 'editable' => false, 'order' => 5, 'width' => 'quarter', 'options' => [['value' => 'male', 'label_ar' => 'ذكر'], ['value' => 'female', 'label_ar' => 'أنثى']]],
                            ['key' => 'spouse_classification', 'type' => 'computed', 'label_ar' => 'التصنيف', 'editable' => false, 'order' => 6, 'width' => 'quarter'],
                            ['key' => 'spouse_nationality', 'type' => 'select_dynamic', 'label_ar' => 'الجنسية', 'required' => true, 'validation' => 'required|string', 'order' => 7, 'width' => 'half', 'data_source' => 'countries', 'default_value' => 'مصري'],
                            ['key' => 'spouse_qualification', 'type' => 'select_dynamic', 'label_ar' => 'المؤهل الدراسي', 'required' => true, 'order' => 8, 'width' => 'half', 'data_source' => 'qualifications'],
                            ['key' => 'spouse_religion', 'type' => 'select', 'label_ar' => 'الديانة', 'required' => true, 'order' => 9, 'width' => 'half', 'options' => [['value' => 'muslim', 'label_ar' => 'مسلم'], ['value' => 'christian', 'label_ar' => 'مسيحي'], ['value' => 'other', 'label_ar' => 'أخرى']]],
                            ['key' => 'spouse_occupation', 'type' => 'text', 'label_ar' => 'الوظيفة', 'required' => false, 'order' => 10, 'width' => 'half'],
                            ['key' => 'spouse_mobile', 'type' => 'phone_eg', 'label_ar' => 'محمول', 'required' => true, 'validation' => 'required|phone_eg', 'order' => 11, 'width' => 'half'],
                            ['key' => 'spouse_work_phone', 'type' => 'phone', 'label_ar' => 'تليفون العمل', 'required' => false, 'order' => 12, 'width' => 'half'],
                            ['key' => 'spouse_work_address', 'type' => 'text', 'label_ar' => 'عنوان العمل', 'required' => false, 'order' => 13, 'width' => 'full'],
                            ['key' => 'marriage_date', 'type' => 'date', 'label_ar' => 'تاريخ الزواج', 'required' => true, 'validation' => 'required|date|date_before_today', 'order' => 14, 'width' => 'half'],
                            ['key' => 'spouse_order', 'type' => 'computed', 'label_ar' => 'ترتيب الزوجة', 'editable' => false, 'order' => 15, 'width' => 'quarter'],
                            ['key' => 'spouse_fee', 'type' => 'currency', 'label_ar' => 'رسوم الإضافة', 'editable' => false, 'order' => 16, 'width' => 'half'],
                            ['key' => 'spouse_fee_breakdown', 'type' => 'static_text', 'label_ar' => 'تفصيل الرسوم', 'editable' => false, 'order' => 17, 'width' => 'full'],
                            ['key' => 'fee_receipt_number', 'type' => 'text', 'label_ar' => 'رقم إيصال الخزينة', 'required' => true, 'validation' => 'required|string', 'order' => 18, 'width' => 'half'],
                        ],
                    ],
                ],
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'form_code'     => 'ADDITION_TEMPORARY',
            'name_ar'       => 'استمارة ضم عضو مؤقت',
            'name_en'       => 'Temporary Member Addition',
            'form_fee'      => '570.00',
            'validity_days' => 15,
            'schema_json'   => json_encode(['sections' => [['key' => 'temp_data', 'label_ar' => 'بيانات العضو المؤقت', 'order' => 1, 'fields' => [
                ['key' => 'temp_name', 'type' => 'text', 'label_ar' => 'الاسم', 'required' => true, 'validation' => 'required|string|min:5|max:200', 'order' => 1, 'width' => 'full'],
                ['key' => 'temp_category', 'type' => 'select', 'label_ar' => 'الفئة', 'required' => true, 'validation' => 'required', 'order' => 2, 'width' => 'half', 'options' => [['value' => 'parent', 'label_ar' => 'والدين'], ['value' => 'special_needs', 'label_ar' => 'ذوي احتياجات خاصة'], ['value' => 'unmarried_daughter', 'label_ar' => 'بنات غير متزوجات'], ['value' => 'sister', 'label_ar' => 'شقيقة'], ['value' => 'stepchild', 'label_ar' => 'أبناء الزوج/الزوجة'], ['value' => 'orphan', 'label_ar' => 'طفل يتيم'], ['value' => 'disabled_sibling', 'label_ar' => 'شقيق معاق'], ['value' => 'nanny', 'label_ar' => 'مربية']]],
                ['key' => 'temp_national_id', 'type' => 'national_id', 'label_ar' => 'الرقم القومي', 'required' => false, 'auto_parse' => true, 'order' => 3, 'width' => 'half'],
                ['key' => 'temp_dob', 'type' => 'date', 'label_ar' => 'تاريخ الميلاد', 'required' => true, 'order' => 4, 'width' => 'quarter'],
                ['key' => 'temp_gender', 'type' => 'select', 'label_ar' => 'النوع', 'required' => true, 'order' => 5, 'width' => 'quarter', 'options' => [['value' => 'male', 'label_ar' => 'ذكر'], ['value' => 'female', 'label_ar' => 'أنثى']]],
                ['key' => 'temp_has_championship', 'type' => 'checkbox', 'label_ar' => 'حاصل على بطولات جمهورية', 'order' => 6, 'width' => 'half'],
                ['key' => 'temp_fee', 'type' => 'currency', 'label_ar' => 'رسوم الإضافة', 'editable' => false, 'order' => 7, 'width' => 'half'],
                ['key' => 'fee_receipt_number', 'type' => 'text', 'label_ar' => 'رقم إيصال الخزينة', 'required' => true, 'order' => 8, 'width' => 'half'],
            ]]]], JSON_UNESCAPED_UNICODE),
        ],
        [
            'form_code'     => 'SEASONAL_MEMBERSHIP',
            'name_ar'       => 'استمارة عضوية موسمية',
            'name_en'       => 'Seasonal Membership Form',
            'form_fee'      => '0.00',
            'validity_days' => null,
            'schema_json'   => json_encode(['sections' => [['key' => 'seasonal_data', 'label_ar' => 'بيانات العضوية الموسمية', 'order' => 1, 'fields' => [
                ['key' => 'seasonal_name', 'type' => 'text', 'label_ar' => 'الاسم', 'required' => true, 'validation' => 'required|string', 'order' => 1, 'width' => 'full'],
                ['key' => 'seasonal_nid', 'type' => 'national_id', 'label_ar' => 'الرقم القومي', 'required' => true, 'auto_parse' => true, 'order' => 2, 'width' => 'half'],
                ['key' => 'seasonal_dob', 'type' => 'date', 'label_ar' => 'تاريخ الميلاد', 'editable' => false, 'order' => 3, 'width' => 'quarter'],
                ['key' => 'seasonal_age', 'type' => 'computed', 'label_ar' => 'السن', 'editable' => false, 'order' => 4, 'width' => 'quarter'],
                ['key' => 'seasonal_qualification', 'type' => 'select_dynamic', 'label_ar' => 'المؤهل', 'required' => true, 'order' => 5, 'width' => 'half', 'data_source' => 'qualifications'],
                ['key' => 'seasonal_phone', 'type' => 'phone_eg', 'label_ar' => 'التليفون', 'required' => true, 'order' => 6, 'width' => 'half'],
                ['key' => 'seasonal_address', 'type' => 'text', 'label_ar' => 'العنوان', 'required' => true, 'order' => 7, 'width' => 'full'],
                ['key' => 'seasonal_fee', 'type' => 'currency', 'label_ar' => 'قيمة الاشتراك', 'editable' => false, 'order' => 8, 'width' => 'half'],
                ['key' => 'seasonal_start', 'type' => 'date', 'label_ar' => 'تاريخ البداية', 'required' => true, 'order' => 9, 'width' => 'quarter'],
                ['key' => 'seasonal_end', 'type' => 'date', 'label_ar' => 'تاريخ النهاية', 'editable' => false, 'order' => 10, 'width' => 'quarter'],
                ['key' => 'seasonal_duration', 'type' => 'static_text', 'label_ar' => 'المدة', 'editable' => false, 'order' => 11, 'width' => 'quarter', 'default_value' => '6 أشهر'],
            ]]]], JSON_UNESCAPED_UNICODE),
        ],
        [
            'form_code'     => 'LOST_CARNET',
            'name_ar'       => 'استمارة بدل فاقد كارنيه',
            'name_en'       => 'Lost Carnet Replacement',
            'form_fee'      => '200.00',
            'validity_days' => null,
            'schema_json'   => json_encode(['sections' => [['key' => 'lost_info', 'label_ar' => 'بيانات البدل الفاقد', 'order' => 1, 'fields' => [
                ['key' => 'member_name', 'type' => 'text', 'label_ar' => 'اسم العضو', 'editable' => false, 'order' => 1, 'width' => 'half'],
                ['key' => 'membership_number', 'type' => 'text', 'label_ar' => 'رقم العضوية', 'editable' => false, 'order' => 2, 'width' => 'quarter'],
                ['key' => 'member_phone', 'type' => 'phone_eg', 'label_ar' => 'رقم المحمول', 'editable' => false, 'order' => 3, 'width' => 'quarter'],
                ['key' => 'loss_declaration', 'type' => 'checkbox', 'label_ar' => 'أقر بفقدان الكارنيه والتعهد بإعادته في حالة العثور عليه', 'required' => true, 'order' => 4, 'width' => 'full'],
                ['key' => 'fee_receipt_number', 'type' => 'text', 'label_ar' => 'رقم إيصال رسوم البدل', 'required' => true, 'order' => 5, 'width' => 'half'],
            ]]]], JSON_UNESCAPED_UNICODE),
        ],
        [
            'form_code'     => 'DATA_UPDATE',
            'name_ar'       => 'استمارة تعديل بيانات',
            'name_en'       => 'Data Update Form',
            'form_fee'      => '0.00',
            'validity_days' => null,
            'schema_json'   => json_encode(['sections' => [['key' => 'update_info', 'label_ar' => 'بيانات التعديل', 'order' => 1, 'fields' => [
                ['key' => 'member_name', 'type' => 'text', 'label_ar' => 'اسم العضو', 'editable' => false, 'order' => 1, 'width' => 'half'],
                ['key' => 'membership_number', 'type' => 'text', 'label_ar' => 'رقم العضوية', 'editable' => false, 'order' => 2, 'width' => 'half'],
                ['key' => 'fields_to_update', 'type' => 'textarea', 'label_ar' => 'الحقول المراد تعديلها', 'required' => true, 'order' => 3, 'width' => 'full'],
                ['key' => 'update_reason', 'type' => 'textarea', 'label_ar' => 'سبب التعديل', 'required' => true, 'order' => 4, 'width' => 'full'],
                ['key' => 'supporting_documents', 'type' => 'file', 'label_ar' => 'المستندات الداعمة', 'required' => false, 'order' => 5, 'width' => 'full'],
            ]]]], JSON_UNESCAPED_UNICODE),
        ],
    ];

    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'] ?? null,
            'form_fee'      => $s['form_fee'],
            'validity_days' => $s['validity_days'],
            'schema_json'   => $s['schema_json'],
            'version'       => 1,
            'published_at'  => $ts,
            'is_active'     => 1,
            'created_at'    => $ts,
            'updated_at'    => $ts,
        ]);
    }
};