<?php

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

return new class extends Migration {

    public function up(): void
    {
        Schema::table('accounts', function (Blueprint $table) {
            $table->string('default_flow', 16)->nullable()->index()->after('type');
        });

        // Backfill existing rows from the old hardcoded rule so behavior is unchanged.
        DB::table('accounts')
            ->whereIn('type', ['Revenue', 'REVENUE', 'Liability', 'LIABILITY'])
            ->update(['default_flow' => 'INCOME']);
        DB::table('accounts')
            ->whereNull('default_flow')
            ->update(['default_flow' => 'EXPENSE']);
    }

    public function down(): void
    {
        Schema::table('accounts', function (Blueprint $table) {
            $table->dropColumn('default_flow');
        });
    }
};
