<?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
{
    /**
     * Run the migrations.
     *
     * expense_category_code was created as an unsignedBigInteger, but shop expense
     * category codes are strings (e.g. "EC-53-00001"). The bigint column silently
     * rejects those values (MySQL strict mode error 1366), so the desktop's category
     * code could never be stored. Convert it to a string to match account_code and
     * the ShopExpenseCategory.code it references.
     */
    public function up(): void
    {
        // Null out any non-string leftovers (e.g. 0) so the type change is clean.
        DB::table('shop_contact_account_transactions')
            ->where('expense_category_code', 0)
            ->update(['expense_category_code' => null]);

        Schema::table('shop_contact_account_transactions', function (Blueprint $table) {
            $table->string('expense_category_code', 50)->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('shop_contact_account_transactions', function (Blueprint $table) {
            $table->unsignedBigInteger('expense_category_code')->nullable()->change();
        });
    }
};
