<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('environment_review_sections', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('environment_review_id')->constrained('environment_reviews')->cascadeOnDelete();
            $table->foreignId('workspace_id')->constrained('workspaces')->cascadeOnDelete();
            $table->foreignId('managed_environment_id')->constrained('managed_environments')->cascadeOnDelete();
            $table->string('section_key');
            $table->string('title');
            $table->unsignedInteger('sort_order')->default(0);
            $table->boolean('required')->default(true);
            $table->string('completeness_state')->default('missing');
            $table->string('source_snapshot_fingerprint', 64)->nullable();
            $table->jsonb('summary_payload')->default('{}');
            $table->jsonb('render_payload')->default('{}');
            $table->timestampTz('measured_at')->nullable();
            $table->timestamps();

            $table->unique(['environment_review_id', 'section_key']);
            $table->index(['workspace_id', 'managed_environment_id', 'section_key']);
            $table->index(['managed_environment_id', 'completeness_state']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('environment_review_sections');
    }
};
