<?php

declare(strict_types=1);

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('offer_sections', function (Blueprint $table): void {
            $table->foreignId('parent_section_id')
                ->nullable()
                ->after('offer_id')
                ->constrained('offer_sections')
                ->cascadeOnDelete();
            $table->string('section_role', 32)->default('chapter')->after('sort_order');
            $table->index(['offer_id', 'parent_section_id', 'sort_order']);
        });

        DB::table('offer_sections')->update(['section_role' => 'chapter']);
    }

    public function down(): void
    {
        Schema::table('offer_sections', function (Blueprint $table): void {
            $table->dropForeign(['parent_section_id']);
            $table->dropColumn(['parent_section_id', 'section_role']);
        });
    }
};
