<?php

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

/**
 * 将历史上重复时间戳的 migration 文件名同步到 migrations 表，避免重命名后被视为未执行。
 *
 * 新装库：UPDATE 命中 0 行，无影响。
 * 已有库：把旧文件名改为新文件名，与重命名后的 PHP 文件一致。
 *
 * 若从旧文件名升级且 `php artisan migrate` 报「列已存在」等重复执行错误，请先单独执行本文件再全量 migrate：
 *   php artisan migrate --path=database/migrations/2026_05_09_119999_rename_duplicate_migration_filenames_in_table.php
 *   php artisan migrate
 */
return new class extends Migration
{
    /** @var array<string, string> */
    private const RENAMES = [
        '2026_05_09_120000_add_username_and_nullable_email_to_admin_users' => '2026_05_09_120001_add_username_and_nullable_email_to_admin_users',
        '2026_05_09_120000_migrate_draw_status_to_domain_dict' => '2026_05_09_120002_migrate_draw_status_to_domain_dict',
        '2026_05_25_120000_consolidate_play_display_name_columns' => '2026_05_25_120001_consolidate_play_display_name_columns',
        '2026_05_25_120000_expand_audit_logs_target_type' => '2026_05_25_120002_expand_audit_logs_target_type',
        '2026_05_25_120000_refine_admin_permission_granularity' => '2026_05_25_120003_refine_admin_permission_granularity',
    ];

    public function up(): void
    {
        foreach (self::RENAMES as $from => $to) {
            DB::table('migrations')->where('migration', $from)->update(['migration' => $to]);
        }
    }

    public function down(): void
    {
        foreach (self::RENAMES as $from => $to) {
            DB::table('migrations')->where('migration', $to)->update(['migration' => $from]);
        }
    }
};
