<?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('policy_versions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();
            $table->foreignId('policy_id')->constrained()->cascadeOnDelete();
            $table->unsignedBigInteger('version_number');
            $table->string('policy_type');
            $table->string('platform')->nullable();
            $table->string('created_by')->nullable();
            $table->timestamp('captured_at')->useCurrent();
            $table->json('snapshot');
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->unique(['policy_id', 'version_number']);
            $table->index(['managed_environment_id', 'policy_type']);
            $table->index('captured_at');
        });
    }

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