<?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('backup_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();
            $table->foreignId('backup_set_id')->constrained()->cascadeOnDelete();
            $table->foreignId('policy_id')->nullable()->constrained()->nullOnDelete();
            $table->string('policy_identifier');
            $table->string('policy_type');
            $table->string('platform')->nullable();
            $table->timestamp('captured_at')->useCurrent();
            $table->json('payload');
            $table->json('metadata')->nullable();
            $table->timestamps();

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

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