<?php

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('baseline_snapshot_items', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('baseline_snapshot_id')->constrained('baseline_snapshots')->cascadeOnDelete();
            $table->string('subject_type');
            $table->string('subject_external_id');
            $table->string('policy_type');
            $table->string('baseline_hash', 64);
            $table->jsonb('meta_jsonb')->nullable();
            $table->timestamps();

            $table->unique(
                ['baseline_snapshot_id', 'subject_type', 'subject_external_id'],
                'baseline_snapshot_items_subject_unique'
            );
            $table->index(['baseline_snapshot_id', 'policy_type'], 'baseline_snapshot_items_policy_type_idx');
        });
    }

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