<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('applicants', function (Blueprint $table) {
            // Personal Information
            $table->string('suffix', 10)->nullable()->after('lastname');
            $table->date('birthdate')->nullable()->after('suffix');
            $table->tinyInteger('age')->nullable()->after('birthdate');
            $table->string('picture', 255)->nullable()->after('age');
            
            // Address Fields - Current Address
            $table->string('region', 100)->nullable()->after('picture');
            $table->string('province', 100)->nullable()->after('region');
            $table->string('city_municipality', 100)->nullable()->after('province');
            $table->string('barangay', 100)->nullable()->after('city_municipality');
            
            // Rename existing address to current_address and increase length
            $table->renameColumn('address', 'current_address');
            
            // After renaming, we'll need to modify the length in a separate migration or use raw SQL
            // For now, adding new address fields
            
            // Address Fields - Permanent Address
            $table->string('permanent_region', 100)->nullable()->after('current_address');
            $table->string('permanent_province', 100)->nullable()->after('permanent_region');
            $table->string('permanent_city_municipality', 100)->nullable()->after('permanent_province');
            $table->string('permanent_barangay', 100)->nullable()->after('permanent_city_municipality');
            $table->string('permanent_address', 250)->nullable()->after('permanent_barangay');
            
            // Address Fields - Provincial Address
            $table->string('provincial_region', 100)->nullable()->after('permanent_address');
            $table->string('provincial_province', 100)->nullable()->after('provincial_region');
            $table->string('provincial_city_municipality', 100)->nullable()->after('provincial_province');
            $table->string('provincial_barangay', 100)->nullable()->after('provincial_city_municipality');
            $table->string('provincial_address', 250)->nullable()->after('provincial_barangay');
            
            // Work Preferences
            $table->string('work_schedule', 50)->nullable()->after('provincial_address');
            
            // Professional Background - Second Position
            $table->string('second_position', 150)->nullable()->after('last_salary');
            $table->string('second_salary', 20)->nullable()->after('second_position');
            
            // HR Questions
            $table->json('hr_questions')->nullable()->after('cover_letter');
            
            // Data Privacy
            $table->boolean('data_privacy_accepted')->default(false)->after('hr_questions');
        });
        
        // Modify existing columns that need length changes
        Schema::table('applicants', function (Blueprint $table) {
            $table->string('email', 100)->change();
            $table->string('last_position', 150)->change();
            $table->string('current_address', 250)->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('applicants', function (Blueprint $table) {
            // Drop new columns
            $table->dropColumn([
                'suffix',
                'birthdate',
                'age',
                'picture',
                'region',
                'province',
                'city_municipality',
                'barangay',
                'permanent_region',
                'permanent_province',
                'permanent_city_municipality',
                'permanent_barangay',
                'permanent_address',
                'provincial_region',
                'provincial_province',
                'provincial_city_municipality',
                'provincial_barangay',
                'provincial_address',
                'work_schedule',
                'second_position',
                'second_salary',
                'hr_questions',
                'data_privacy_accepted'
            ]);
            
            // Rename current_address back to address
            $table->renameColumn('current_address', 'address');
        });
        
        // Revert column changes
        Schema::table('applicants', function (Blueprint $table) {
            $table->string('email', 20)->change();
            $table->string('last_position', 20)->change();
            $table->string('address', 250)->change();
        });
    }
};