<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Postgres supports IF NOT EXISTS on CREATE INDEX, so the migration is idempotent
        // without juggling Laravel's Blueprint-level checks (which lose $this in closures).
        DB::statement('CREATE INDEX IF NOT EXISTS password_histories_user_id_created_at_index ON password_histories (user_id, created_at)');
        DB::statement('CREATE INDEX IF NOT EXISTS system_setting_revisions_key_created_at_index ON system_setting_revisions (key, created_at)');
        DB::statement('CREATE INDEX IF NOT EXISTS system_setting_revisions_changed_by_index ON system_setting_revisions (changed_by)');
        DB::statement('CREATE INDEX IF NOT EXISTS notifications_notifiable_read_at_index ON notifications (notifiable_type, notifiable_id, read_at)');
    }

    public function down(): void
    {
        DB::statement('DROP INDEX IF EXISTS password_histories_user_id_created_at_index');
        DB::statement('DROP INDEX IF EXISTS system_setting_revisions_key_created_at_index');
        DB::statement('DROP INDEX IF EXISTS system_setting_revisions_changed_by_index');
        DB::statement('DROP INDEX IF EXISTS notifications_notifiable_read_at_index');
    }
};
