<?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('users', function (Blueprint $table) {
            if (! Schema::hasColumn('users', 'province')) {
                $table->string('province', 2)->nullable()->after('address');
            }
            if (! Schema::hasColumn('users', 'postal_code')) {
                $table->string('postal_code', 20)->nullable()->after('province');
            }
            if (! Schema::hasColumn('users', 'sin_last4')) {
                $table->string('sin_last4', 4)->nullable()->after('postal_code');
            }
            if (! Schema::hasColumn('users', 'tax_province')) {
                $table->string('tax_province', 2)->nullable()->after('sin_last4');
            }
            if (! Schema::hasColumn('users', 'work_authorization_status')) {
                $table->string('work_authorization_status')->nullable()->after('tax_province');
            }
            if (! Schema::hasColumn('users', 'payroll_frequency')) {
                $table->string('payroll_frequency')->default('biweekly')->after('basic_salary');
            }
            if (! Schema::hasColumn('users', 'bank_institution_number')) {
                $table->string('bank_institution_number', 10)->nullable()->after('bank_name');
            }
            if (! Schema::hasColumn('users', 'bank_transit_number')) {
                $table->string('bank_transit_number', 10)->nullable()->after('bank_institution_number');
            }
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            foreach ([
                'bank_transit_number',
                'bank_institution_number',
                'payroll_frequency',
                'work_authorization_status',
                'tax_province',
                'sin_last4',
                'postal_code',
                'province',
            ] as $column) {
                if (Schema::hasColumn('users', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
