<?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('tenant_review_sections', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_review_id')->constrained('tenant_reviews')->cascadeOnDelete();
            $table->foreignId('workspace_id')->constrained('workspaces')->cascadeOnDelete();
            $table->foreignId('tenant_id')->constrained('tenants')->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(['tenant_review_id', 'section_key']);
            $table->index(['workspace_id', 'tenant_id', 'section_key']);
            $table->index(['tenant_id', 'completeness_state']);
        });
    }

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