<?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('salaries', function (Blueprint $table) {
            if (!Schema::hasColumn('salaries', 'paid_at')) {
                $table->dateTime('paid_at')->nullable()->after('status');
            }

            if (!Schema::hasColumn('salaries', 'payment_method')) {
                $table->string('payment_method', 20)->nullable()->after('paid_at');
            }

            if (!Schema::hasColumn('salaries', 'payment_source')) {
                $table->string('payment_source', 20)->nullable()->after('payment_method');
            }

            if (!Schema::hasColumn('salaries', 'payment_account_id')) {
                $table->unsignedBigInteger('payment_account_id')->nullable()->after('payment_source');
            }

            if (!Schema::hasColumn('salaries', 'payment_notes')) {
                $table->text('payment_notes')->nullable()->after('payment_account_id');
            }

            if (!Schema::hasColumn('salaries', 'paid_by')) {
                $table->unsignedBigInteger('paid_by')->nullable()->after('payment_notes');
            }
        });
    }

    public function down(): void
    {
        Schema::table('salaries', function (Blueprint $table) {
            foreach ([
                'paid_at',
                'payment_method',
                'payment_source',
                'payment_account_id',
                'payment_notes',
                'paid_by',
            ] as $column) {
                if (Schema::hasColumn('salaries', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};