<?php

use Hyperf\Database\Schema\Blueprint;
use Hypervel\Database\Migrations\Migration;
use Hypervel\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('user_infos', function (Blueprint $table) {
            // Personal Info
            if (!Schema::hasColumn('user_infos', 'firstname')) $table->string('firstname')->nullable();
            if (!Schema::hasColumn('user_infos', 'middlename')) $table->string('middlename')->nullable();
            if (!Schema::hasColumn('user_infos', 'lastname')) $table->string('lastname')->nullable();
            $table->string('suffix')->nullable();
            $table->string('gender')->nullable();
            $table->date('dob')->nullable();
            $table->string('priority_sector')->nullable();

            // Social Accounts
            $table->string('messenger_id')->nullable();
            $table->string('viber_number')->nullable();
            $table->string('tiktok_username')->nullable();

            // Address Fragments
            $table->string('region')->nullable();
            $table->string('province')->nullable();
            $table->string('city')->nullable();
            $table->string('barangay')->nullable();

            // Family & Education
            $table->string('civil_status')->nullable();
            $table->integer('children_count')->default(0);
            $table->integer('dependent_count')->nullable();
            $table->string('education_level')->nullable();
            $table->string('course')->nullable();
            $table->string('school')->nullable();
            $table->string('year_last_attended')->nullable();

            // Livelihood
            $table->string('livelihood_source')->nullable();
            $table->string('last_company')->nullable();
            $table->string('employer_name')->nullable();
            $table->string('last_position')->nullable();
            $table->string('occupation')->nullable();
            $table->decimal('monthly_income', 15, 2)->nullable();
            $table->string('last_employment_year')->nullable();

            // Government Info
            $table->string('tin')->nullable();
            $table->string('philhealth_id')->nullable();
            $table->string('gov_id')->nullable();
            $table->string('id_type')->nullable();
            $table->string('id_number')->nullable();
            $table->string('beneficiary_type')->nullable();

            // Emergency Contact Info
            $table->string('emergency_contact_name')->nullable();
            $table->string('emergency_contact_address')->nullable();
            $table->string('emergency_contact_phone')->nullable();
            $table->string('emergency_contact_relation')->nullable();
            $table->integer('emergency_contact_user_id')->nullable();

            // Financial Info
            $table->string('bank_account_no')->nullable();

            // Note: emergency_contact_user_id is not set as actual FK to avoid issues if the user is deleted
            // but we'll treat it as such in the model.
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('user_infos', function (Blueprint $table) {
            $table->dropColumn([
                'firstname', 'middlename', 'lastname', 'suffix', 'gender', 'dob',
                'messenger_id', 'viber_number', 'tiktok_username',
                'region', 'province', 'city', 'barangay',
                'civil_status', 'children_count', 'education_level', 'course', 'school', 'year_last_attended',
                'livelihood_source', 'last_company', 'last_position', 'last_employment_year',
                'tin', 'philhealth_id', 'gov_id', 'id_type', 'id_number', 'beneficiary_type',
                'priority_sector', 'employer_name', 'occupation', 'monthly_income', 'dependent_count',
                'bank_account_no',
                'emergency_contact_name', 'emergency_contact_address', 'emergency_contact_phone', 'emergency_contact_relation', 'emergency_contact_user_id'
            ]);
        });
    }
};
