<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Idempotency guard: if schema.sql was loaded first, the table already exists
        // (as a partitioned table from hole #2 migration). Skip creation in that case.
        // The 2026_05_23_000002_partition_audit_tables migration will handle the partitioned
        // form when it runs.
        if (DB::selectOne('SELECT 1 AS ok FROM pg_class WHERE relname = ?', ['tenant_operations_log']) !== null) {
            return;
        }

        $sql = file_get_contents(base_path('../db/migrations/2026_05_22_001_tenant_operations_log.sql'));
        if ($sql === false) {
            throw new RuntimeException('Migration SQL file not found.');
        }
        DB::unprepared($sql);
    }

    public function down(): void
    {
        DB::unprepared('DROP TABLE IF EXISTS tenant_operations_log CASCADE;');
    }
};
