<?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('policies', function (Blueprint $table) {
            $table->id();
            $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();
            $table->string('external_id');
            $table->string('policy_type');
            $table->string('platform')->nullable();
            $table->string('display_name');
            $table->timestamp('last_synced_at')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->unique(['managed_environment_id', 'external_id', 'policy_type']);
            $table->index(['managed_environment_id', 'policy_type', 'platform']);
            $table->index('last_synced_at');
        });
    }

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