<?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
    {
        if (! Schema::hasTable('policy_versions')) {
            return;
        }

        Schema::table('policy_versions', function (Blueprint $table): void {
            if (! Schema::hasColumn('policy_versions', 'capture_purpose')) {
                $table->string('capture_purpose')->default('backup')->after('scope_tags_hash');
            }

            if (! Schema::hasColumn('policy_versions', 'operation_run_id')) {
                $table->unsignedBigInteger('operation_run_id')->nullable()->after('capture_purpose');
            }

            if (! Schema::hasColumn('policy_versions', 'baseline_profile_id')) {
                $table->unsignedBigInteger('baseline_profile_id')->nullable()->after('operation_run_id');
            }
        });

        Schema::table('policy_versions', function (Blueprint $table): void {
            if (! Schema::hasColumn('policy_versions', 'capture_purpose')) {
                return;
            }

            $table->index(['managed_environment_id', 'policy_id', 'capture_purpose', 'captured_at'], 'policy_versions_tenant_policy_purpose_captured_idx');

            if (Schema::hasColumn('policy_versions', 'operation_run_id')) {
                $table->index(['managed_environment_id', 'capture_purpose', 'operation_run_id'], 'policy_versions_tenant_purpose_run_idx');
                $table->foreign('operation_run_id')->references('id')->on('operation_runs')->nullOnDelete();
            }

            if (Schema::hasColumn('policy_versions', 'baseline_profile_id')) {
                $table->index(['managed_environment_id', 'capture_purpose', 'baseline_profile_id'], 'policy_versions_tenant_purpose_profile_idx');
                $table->foreign('baseline_profile_id')->references('id')->on('baseline_profiles')->nullOnDelete();
            }
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('policy_versions')) {
            return;
        }

        Schema::table('policy_versions', function (Blueprint $table): void {
            if (Schema::hasColumn('policy_versions', 'baseline_profile_id')) {
                $table->dropForeign(['baseline_profile_id']);
                $table->dropIndex('policy_versions_tenant_purpose_profile_idx');
                $table->dropColumn('baseline_profile_id');
            }

            if (Schema::hasColumn('policy_versions', 'operation_run_id')) {
                $table->dropForeign(['operation_run_id']);
                $table->dropIndex('policy_versions_tenant_purpose_run_idx');
                $table->dropColumn('operation_run_id');
            }

            if (Schema::hasColumn('policy_versions', 'capture_purpose')) {
                $table->dropIndex('policy_versions_tenant_policy_purpose_captured_idx');
                $table->dropColumn('capture_purpose');
            }
        });
    }
};
