<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('finding_exceptions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('workspace_id')->constrained('workspaces')->cascadeOnDelete();
            $table->foreignId('managed_environment_id')->constrained('managed_environments')->cascadeOnDelete();
            $table->foreignId('finding_id')->constrained('findings')->cascadeOnDelete();
            $table->foreignId('requested_by_user_id')->constrained('users');
            $table->foreignId('owner_user_id')->nullable()->constrained('users');
            $table->foreignId('approved_by_user_id')->nullable()->constrained('users');
            $table->unsignedBigInteger('current_decision_id')->nullable();
            $table->string('status')->default('pending');
            $table->string('current_validity_state')->default('missing_support');
            $table->text('request_reason');
            $table->text('approval_reason')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->text('revocation_reason')->nullable();
            $table->timestampTz('requested_at')->nullable();
            $table->timestampTz('approved_at')->nullable();
            $table->timestampTz('rejected_at')->nullable();
            $table->timestampTz('revoked_at')->nullable();
            $table->timestampTz('effective_from')->nullable();
            $table->timestampTz('expires_at')->nullable();
            $table->timestampTz('review_due_at')->nullable();
            $table->jsonb('evidence_summary')->default('{}');
            $table->timestamps();

            $table->unique('finding_id');
            $table->index(['workspace_id', 'status', 'review_due_at'], 'finding_exceptions_workspace_status_review_idx');
            $table->index(['workspace_id', 'status', 'expires_at'], 'finding_exceptions_workspace_status_expiry_idx');
            $table->index(['workspace_id', 'managed_environment_id', 'status'], 'finding_exceptions_tenant_status_idx');
            $table->index(['managed_environment_id', 'finding_id'], 'finding_exceptions_finding_lookup_idx');
            $table->index(['managed_environment_id', 'requested_at'], 'finding_exceptions_requested_at_idx');

            $table
                ->foreign(['managed_environment_id', 'workspace_id'], 'finding_exceptions_managed_environment_workspace_fk')
                ->references(['id', 'workspace_id'])
                ->on('managed_environments')
                ->cascadeOnDelete();
        });

        if (DB::getDriverName() === 'pgsql') {
            DB::statement('CREATE INDEX finding_exceptions_evidence_summary_gin ON finding_exceptions USING GIN (evidence_summary)');
        }
    }

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