<?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('content_types', function (Blueprint $table): void {
            $table->unsignedInteger('order')->default(0)->after('description');
            $table->index('order');
        });

        $ids = DB::table('content_types')
            ->orderBy('id')
            ->pluck('id');

        foreach ($ids as $index => $id) {
            DB::table('content_types')
                ->where('id', $id)
                ->update(['order' => $index + 1]);
        }
    }

    public function down(): void
    {
        Schema::table('content_types', function (Blueprint $table): void {
            $table->dropIndex(['order']);
            $table->dropColumn('order');
        });
    }
};