<?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', 'secret_fingerprints')) {
                $table->json('secret_fingerprints')->nullable()->after('scope_tags_hash');
            }

            if (! Schema::hasColumn('policy_versions', 'redaction_version')) {
                $table->unsignedInteger('redaction_version')->nullable()->after('secret_fingerprints');
            }
        });
    }

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

        Schema::table('policy_versions', function (Blueprint $table): void {
            if (Schema::hasColumn('policy_versions', 'redaction_version')) {
                $table->dropColumn('redaction_version');
            }

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