<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('loan_transactions', function (Blueprint $table) {
            $table->string('source', 20)->default('payroll')->after('payroll_id');
            $table->string('note', 255)->nullable()->after('source');
            $table->foreignId('recorded_by')->nullable()->after('note')->constrained('users')->nullOnDelete();
        });

        DB::table('loan_transactions')
            ->whereNull('payroll_id')
            ->update(['source' => 'manual']);
    }

    public function down(): void
    {
        Schema::table('loan_transactions', function (Blueprint $table) {
            $table->dropConstrainedForeignId('recorded_by');
            $table->dropColumn('note');
            $table->dropColumn('source');
        });
    }
};
