<?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.
     */
    public function up(): void
    {
        Schema::table('server_resources', function (Blueprint $table) {
            $table->string('display_name')->nullable()->after('name');
            $table->unsignedBigInteger('asset_id')->nullable()->change();
            $table->unsignedBigInteger('account_id')->nullable()->change();
        });

        DB::table('server_resources')->update([
            'display_name' => DB::raw('name'),
        ]);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        DB::table('server_resources')
            ->whereNull('asset_id')
            ->orWhereNull('account_id')
            ->delete();

        Schema::table('server_resources', function (Blueprint $table) {
            $table->dropColumn('display_name');
            $table->unsignedBigInteger('asset_id')->nullable(false)->change();
            $table->unsignedBigInteger('account_id')->nullable(false)->change();
        });
    }
};
