<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('employees', function (Blueprint $table) {
            $table->string('middle_name')->nullable();
            $table->string('display_name')->nullable();
            $table->string('gender', 20)->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('nationality', 80)->nullable();
            $table->string('marital_status', 30)->nullable();
            $table->string('profile_photo_path')->nullable();
            $table->string('personal_mobile', 30)->nullable();
            $table->string('personal_email')->nullable();
            $table->text('uae_address')->nullable();
            $table->text('home_country_address')->nullable();
            $table->string('emergency_contact_name')->nullable();
            $table->string('emergency_contact_relation', 80)->nullable();
            $table->string('emergency_contact_mobile', 30)->nullable();
            $table->unsignedBigInteger('reporting_manager_id')->nullable()->index();
            $table->date('probation_start')->nullable();
            $table->date('probation_end')->nullable();
            $table->date('contract_start')->nullable();
            $table->date('contract_end')->nullable();
            $table->string('work_location')->nullable();
            $table->string('weekly_off', 30)->nullable();
            $table->string('payroll_group_code', 60)->nullable();
            $table->string('passport_number', 60)->nullable();
            $table->date('passport_issue_date')->nullable();
            $table->string('passport_country', 80)->nullable();
            $table->date('emirates_id_issue_date')->nullable();
            $table->date('emirates_id_expiry')->nullable();
            $table->string('visa_number', 80)->nullable();
            $table->string('visa_file_number', 80)->nullable();
            $table->date('visa_issue_date')->nullable();
            $table->string('visa_type', 80)->nullable();
            $table->string('sponsor')->nullable();
            $table->string('work_permit_number', 80)->nullable();
            $table->date('work_permit_issue_date')->nullable();
            $table->date('work_permit_expiry')->nullable();
            $table->string('labour_card_number', 80)->nullable();
            $table->string('insurance_provider')->nullable();
            $table->string('insurance_number', 80)->nullable();
            $table->date('insurance_expiry')->nullable();
            $table->index(['tenant_id', 'status', 'joining_date'], 'idx_employees_hr_status');
            $table->index(['tenant_id', 'passport_number'], 'idx_employees_passport');
            $table->index(['tenant_id', 'emirates_id'], 'idx_employees_eid');
        });

        Schema::table('employee_documents', function (Blueprint $table) {
            $table->unsignedBigInteger('parent_document_id')->nullable()->index();
            $table->unsignedInteger('version_number')->default(1);
            $table->boolean('is_current')->default(true)->index();
            $table->string('status', 30)->default('pending_verification');
            $table->unsignedBigInteger('verified_by')->nullable()->index();
            $table->timestamp('verified_at')->nullable();
            $table->text('notes')->nullable();
            $table->index(['tenant_id', 'employee_id', 'document_type', 'is_current'], 'idx_employee_doc_current');
            $table->index(['tenant_id', 'expiry_date', 'is_current'], 'idx_employee_doc_expiry');
        });

        Schema::create('designations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('departments')->nullOnDelete();
            $table->string('name');
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->unique(['tenant_id', 'department_id', 'name']);
        });

        Schema::create('shifts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('departments')->nullOnDelete();
            $table->string('name');
            $table->string('code', 40);
            $table->string('type', 30)->default('day');
            $table->time('start_time')->nullable();
            $table->time('end_time')->nullable();
            $table->time('second_start_time')->nullable();
            $table->time('second_end_time')->nullable();
            $table->unsignedInteger('grace_minutes')->default(0);
            $table->unsignedInteger('break_minutes')->default(0);
            $table->unsignedInteger('minimum_overtime_minutes')->default(30);
            $table->decimal('overtime_multiplier', 6, 3)->default(1.25);
            $table->json('applicable_days')->nullable();
            $table->json('late_rule')->nullable();
            $table->json('early_leave_rule')->nullable();
            $table->json('overtime_rule')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['tenant_id', 'code']);
        });

        Schema::create('shift_assignments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('shift_id')->constrained('shifts')->restrictOnDelete();
            $table->foreignId('employee_id')->nullable()->constrained('employees')->cascadeOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('departments')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->cascadeOnDelete();
            $table->date('effective_from');
            $table->date('effective_to')->nullable();
            $table->foreignId('assigned_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->index(['tenant_id', 'employee_id', 'effective_from', 'effective_to'], 'idx_shift_assignment_employee');
        });

        Schema::create('attendance_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->foreignId('shift_id')->nullable()->constrained('shifts')->nullOnDelete();
            $table->date('attendance_date');
            $table->dateTime('scheduled_in')->nullable();
            $table->dateTime('scheduled_out')->nullable();
            $table->dateTime('actual_check_in')->nullable();
            $table->dateTime('actual_check_out')->nullable();
            $table->unsignedInteger('worked_minutes')->default(0);
            $table->unsignedInteger('break_minutes')->default(0);
            $table->unsignedInteger('late_minutes')->default(0);
            $table->unsignedInteger('early_leave_minutes')->default(0);
            $table->unsignedInteger('overtime_minutes')->default(0);
            $table->string('status', 30)->default('present');
            $table->string('source', 30)->default('manual');
            $table->string('external_reference')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'employee_id', 'attendance_date']);
            $table->index(['tenant_id', 'branch_id', 'attendance_date', 'status'], 'idx_attendance_dashboard');
        });

        Schema::create('attendance_corrections', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('attendance_record_id')->constrained('attendance_records')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->dateTime('original_check_in')->nullable();
            $table->dateTime('original_check_out')->nullable();
            $table->dateTime('requested_check_in')->nullable();
            $table->dateTime('requested_check_out')->nullable();
            $table->string('requested_status', 30)->nullable();
            $table->text('reason');
            $table->string('status', 20)->default('requested');
            $table->foreignId('requested_by')->constrained('users')->restrictOnDelete();
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->text('review_comment')->nullable();
            $table->timestamps();
            $table->index(['tenant_id', 'status', 'created_at']);
        });

        Schema::create('leave_types', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('name');
            $table->string('code', 40);
            $table->decimal('annual_allocation', 8, 2)->default(0);
            $table->decimal('maximum_carry_forward', 8, 2)->default(0);
            $table->string('accrual_method', 30)->default('annual');
            $table->boolean('is_paid')->default(true);
            $table->boolean('requires_attachment')->default(false);
            $table->unsignedTinyInteger('approval_levels')->default(1);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->unique(['tenant_id', 'code']);
        });

        Schema::create('leave_balances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->foreignId('leave_type_id')->constrained('leave_types')->cascadeOnDelete();
            $table->unsignedInteger('year');
            $table->decimal('opening_balance', 8, 2)->default(0);
            $table->decimal('accrued', 8, 2)->default(0);
            $table->decimal('used', 8, 2)->default(0);
            $table->decimal('adjusted', 8, 2)->default(0);
            $table->timestamps();
            $table->unique(['employee_id', 'leave_type_id', 'year']);
        });

        Schema::create('leave_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();
            $table->string('request_number', 60);
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->foreignId('leave_type_id')->constrained('leave_types')->restrictOnDelete();
            $table->date('from_date');
            $table->date('to_date');
            $table->decimal('total_days', 8, 2);
            $table->text('reason');
            $table->string('contact_during_leave')->nullable();
            $table->string('attachment_path')->nullable();
            $table->string('status', 30)->default('submitted');
            $table->unsignedTinyInteger('current_approval_level')->default(1);
            $table->foreignId('requested_by')->constrained('users')->restrictOnDelete();
            $table->foreignId('manager_approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('manager_approved_at')->nullable();
            $table->foreignId('hr_approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('hr_approved_at')->nullable();
            $table->foreignId('rejected_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('rejected_at')->nullable();
            $table->text('review_comment')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['tenant_id', 'request_number']);
            $table->index(['tenant_id', 'status', 'from_date', 'to_date'], 'idx_leave_workflow');
        });

        Schema::create('salary_components', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('name');
            $table->string('code', 50);
            $table->string('type', 20);
            $table->string('calculation_type', 30)->default('fixed');
            $table->boolean('is_taxable')->default(false);
            $table->boolean('include_in_gratuity')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->unique(['tenant_id', 'code']);
        });

        Schema::create('payroll_groups', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('name');
            $table->string('code', 40);
            $table->unsignedTinyInteger('pay_day')->default(28);
            $table->string('frequency', 20)->default('monthly');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->unique(['tenant_id', 'code']);
        });

        Schema::create('employee_salary_structures', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->foreignId('payroll_group_id')->nullable()->constrained('payroll_groups')->nullOnDelete();
            $table->date('effective_from');
            $table->date('effective_to')->nullable();
            $table->decimal('basic_salary', 14, 2);
            $table->decimal('housing_allowance', 14, 2)->default(0);
            $table->decimal('transport_allowance', 14, 2)->default(0);
            $table->decimal('other_fixed_allowance', 14, 2)->default(0);
            $table->decimal('variable_allowance', 14, 2)->default(0);
            $table->boolean('overtime_eligible')->default(false);
            $table->decimal('overtime_hourly_rate', 12, 4)->nullable();
            $table->string('currency', 3)->default('AED');
            $table->string('payment_method', 30)->default('bank_transfer');
            $table->json('components_json')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('change_reason')->nullable();
            $table->timestamps();
            $table->index(['tenant_id', 'employee_id', 'effective_from', 'effective_to'], 'idx_salary_effective');
        });

        Schema::create('employee_bank_details', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->text('bank_name_encrypted')->nullable();
            $table->text('account_holder_encrypted')->nullable();
            $table->text('iban_encrypted')->nullable();
            $table->text('account_number_encrypted')->nullable();
            $table->text('routing_code_encrypted')->nullable();
            $table->text('wps_employee_id_encrypted')->nullable();
            $table->string('establishment_reference')->nullable();
            $table->boolean('is_verified')->default(false);
            $table->foreignId('verified_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('verified_at')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'employee_id']);
        });

        Schema::create('employee_loans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('loan_number', 60);
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->string('type', 30)->default('loan');
            $table->decimal('amount', 14, 2);
            $table->date('loan_date');
            $table->decimal('installment_amount', 14, 2);
            $table->date('start_month');
            $table->decimal('remaining_amount', 14, 2);
            $table->string('status', 20)->default('requested');
            $table->text('notes')->nullable();
            $table->foreignId('requested_by')->constrained('users')->restrictOnDelete();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['tenant_id', 'loan_number']);
        });

        Schema::create('payroll_periods', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('period_name');
            $table->date('period_start');
            $table->date('period_end');
            $table->date('payment_date')->nullable();
            $table->string('status', 20)->default('open');
            $table->timestamps();
            $table->unique(['tenant_id', 'period_start', 'period_end']);
        });

        Schema::create('payroll_runs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();
            $table->foreignId('payroll_period_id')->constrained('payroll_periods')->restrictOnDelete();
            $table->foreignId('payroll_group_id')->nullable()->constrained('payroll_groups')->nullOnDelete();
            $table->string('run_number', 60);
            $table->string('status', 30)->default('draft');
            $table->decimal('gross_total', 16, 2)->default(0);
            $table->decimal('deduction_total', 16, 2)->default(0);
            $table->decimal('net_total', 16, 2)->default(0);
            $table->json('validation_warnings')->nullable();
            $table->foreignId('created_by')->constrained('users')->restrictOnDelete();
            $table->foreignId('hr_reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('hr_reviewed_at')->nullable();
            $table->foreignId('finance_reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('finance_reviewed_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->foreignId('paid_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('paid_at')->nullable();
            $table->timestamp('locked_at')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'run_number']);
            $table->index(['tenant_id', 'payroll_period_id', 'status']);
        });

        Schema::create('payroll_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('payroll_run_id')->constrained('payroll_runs')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->restrictOnDelete();
            $table->foreignId('salary_structure_id')->constrained('employee_salary_structures')->restrictOnDelete();
            $table->decimal('basic_salary', 14, 2);
            $table->decimal('housing_allowance', 14, 2)->default(0);
            $table->decimal('transport_allowance', 14, 2)->default(0);
            $table->decimal('other_earnings', 14, 2)->default(0);
            $table->decimal('overtime_amount', 14, 2)->default(0);
            $table->decimal('bonus_amount', 14, 2)->default(0);
            $table->decimal('gross_salary', 14, 2);
            $table->decimal('absence_deduction', 14, 2)->default(0);
            $table->decimal('unpaid_leave_deduction', 14, 2)->default(0);
            $table->decimal('loan_deduction', 14, 2)->default(0);
            $table->decimal('other_deductions', 14, 2)->default(0);
            $table->decimal('total_deductions', 14, 2)->default(0);
            $table->decimal('net_salary', 14, 2);
            $table->unsignedInteger('working_days')->default(0);
            $table->unsignedInteger('present_days')->default(0);
            $table->decimal('unpaid_leave_days', 8, 2)->default(0);
            $table->unsignedInteger('absence_days')->default(0);
            $table->unsignedInteger('overtime_minutes')->default(0);
            $table->json('calculation_snapshot');
            $table->string('status', 20)->default('calculated');
            $table->timestamps();
            $table->unique(['payroll_run_id', 'employee_id']);
        });

        Schema::create('payroll_item_components', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('payroll_item_id')->constrained('payroll_items')->cascadeOnDelete();
            $table->foreignId('salary_component_id')->nullable()->constrained('salary_components')->nullOnDelete();
            $table->string('name');
            $table->string('type', 20);
            $table->decimal('amount', 14, 2);
            $table->string('source', 30)->default('calculated');
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('payroll_adjustments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('payroll_item_id')->constrained('payroll_items')->cascadeOnDelete();
            $table->string('type', 20);
            $table->decimal('amount', 14, 2);
            $table->text('reason');
            $table->foreignId('created_by')->constrained('users')->restrictOnDelete();
            $table->timestamps();
        });

        Schema::create('loan_installments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_loan_id')->constrained('employee_loans')->cascadeOnDelete();
            $table->foreignId('payroll_item_id')->nullable()->constrained('payroll_items')->nullOnDelete();
            $table->date('due_month');
            $table->decimal('amount', 14, 2);
            $table->string('status', 20)->default('scheduled');
            $table->timestamp('deducted_at')->nullable();
            $table->timestamps();
            $table->unique(['employee_loan_id', 'due_month']);
        });

        Schema::create('salary_payment_batches', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('payroll_run_id')->constrained('payroll_runs')->restrictOnDelete();
            $table->string('batch_number', 60);
            $table->string('format', 30)->default('wps_sif');
            $table->string('status', 20)->default('prepared');
            $table->string('file_path')->nullable();
            $table->string('submission_reference')->nullable();
            $table->text('response_notes')->nullable();
            $table->foreignId('prepared_by')->constrained('users')->restrictOnDelete();
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('processed_at')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'batch_number']);
        });

        Schema::create('employee_gratuities', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->restrictOnDelete();
            $table->date('joining_date');
            $table->date('last_working_date');
            $table->decimal('basic_salary', 14, 2);
            $table->unsignedInteger('eligible_service_days');
            $table->unsignedInteger('excluded_days')->default(0);
            $table->string('termination_reason', 80)->nullable();
            $table->decimal('estimated_amount', 14, 2);
            $table->decimal('approved_amount', 14, 2)->nullable();
            $table->json('calculation_snapshot');
            $table->string('status', 20)->default('estimated');
            $table->foreignId('calculated_by')->constrained('users')->restrictOnDelete();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
        });

        Schema::create('final_settlements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->restrictOnDelete();
            $table->foreignId('employee_gratuity_id')->nullable()->constrained('employee_gratuities')->nullOnDelete();
            $table->decimal('salary_due', 14, 2)->default(0);
            $table->decimal('leave_salary', 14, 2)->default(0);
            $table->decimal('gratuity_amount', 14, 2)->default(0);
            $table->decimal('other_earnings', 14, 2)->default(0);
            $table->decimal('loan_balance', 14, 2)->default(0);
            $table->decimal('other_deductions', 14, 2)->default(0);
            $table->decimal('net_settlement', 14, 2);
            $table->string('status', 20)->default('draft');
            $table->foreignId('created_by')->constrained('users')->restrictOnDelete();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();
        });

        foreach (['hr_settings', 'attendance_settings', 'payroll_settings'] as $settingsTable) {
            Schema::create($settingsTable, function (Blueprint $table) {
                $table->id();
                $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
                $table->json('settings')->nullable();
                $table->timestamps();
                $table->unique('tenant_id');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('payroll_settings');
        Schema::dropIfExists('attendance_settings');
        Schema::dropIfExists('hr_settings');
        Schema::dropIfExists('final_settlements');
        Schema::dropIfExists('employee_gratuities');
        Schema::dropIfExists('salary_payment_batches');
        Schema::dropIfExists('loan_installments');
        Schema::dropIfExists('payroll_adjustments');
        Schema::dropIfExists('payroll_item_components');
        Schema::dropIfExists('payroll_items');
        Schema::dropIfExists('payroll_runs');
        Schema::dropIfExists('payroll_periods');
        Schema::dropIfExists('employee_loans');
        Schema::dropIfExists('employee_bank_details');
        Schema::dropIfExists('employee_salary_structures');
        Schema::dropIfExists('payroll_groups');
        Schema::dropIfExists('salary_components');
        Schema::dropIfExists('leave_requests');
        Schema::dropIfExists('leave_balances');
        Schema::dropIfExists('leave_types');
        Schema::dropIfExists('attendance_corrections');
        Schema::dropIfExists('attendance_records');
        Schema::dropIfExists('shift_assignments');
        Schema::dropIfExists('shifts');
        Schema::dropIfExists('designations');

        Schema::table('employee_documents', function (Blueprint $table) {
            $table->dropColumn(['parent_document_id', 'version_number', 'is_current', 'status', 'verified_by', 'verified_at', 'notes']);
        });

        Schema::table('employees', function (Blueprint $table) {
            $table->dropColumn([
                'middle_name', 'display_name', 'gender', 'date_of_birth', 'nationality', 'marital_status',
                'profile_photo_path', 'personal_mobile', 'personal_email', 'uae_address', 'home_country_address',
                'emergency_contact_name', 'emergency_contact_relation', 'emergency_contact_mobile',
                'reporting_manager_id', 'probation_start', 'probation_end', 'contract_start', 'contract_end',
                'work_location', 'weekly_off', 'payroll_group_code', 'passport_number', 'passport_issue_date',
                'passport_country', 'emirates_id_issue_date', 'emirates_id_expiry', 'visa_number',
                'visa_file_number', 'visa_issue_date', 'visa_type', 'sponsor', 'work_permit_number',
                'work_permit_issue_date', 'work_permit_expiry', 'labour_card_number', 'insurance_provider',
                'insurance_number', 'insurance_expiry',
            ]);
        });
    }
};
