<?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('expenses', function (Blueprint $table) {
            $table->string('name')->nullable()->after('name_en');
            $table->text('details')->nullable()->after('details_en');
            $table->string('custom_type')->nullable()->after('custom_type_en');
        });

        DB::table('expenses')->whereNull('name')->update([
            'name' => DB::raw('COALESCE(name_ar, name_en)'),
        ]);

        DB::table('expenses')->whereNull('details')->update([
            'details' => DB::raw('COALESCE(details_ar, details_en)'),
        ]);

        DB::table('expenses')->whereNull('custom_type')->update([
            'custom_type' => DB::raw('COALESCE(custom_type_ar, custom_type_en)'),
        ]);
    }

    public function down(): void
    {
        Schema::table('expenses', function (Blueprint $table) {
            $table->dropColumn(['name', 'details', 'custom_type']);
        });
    }
};
