<?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('inventory_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('managed_environment_id')->constrained();
            $table->string('policy_type');
            $table->string('external_id');
            $table->string('display_name')->nullable();
            $table->string('category')->nullable();
            $table->string('platform')->nullable();
            $table->jsonb('meta_jsonb')->nullable();
            $table->timestampTz('last_seen_at')->nullable();
            $table->foreignId('last_seen_run_id')
                ->nullable()
                ->constrained('inventory_sync_runs')
                ->nullOnDelete();
            $table->timestamps();

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

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