<?php

/**
 * 文件功能：清理已废弃的等级阈值聊天室管理权限参数
 * 统一删除 level_* 权限残留，避免继续与职务权限体系并存。
 */

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

return new class extends Migration
{
    /**
     * 方法功能：删除旧等级权限参数。
     */
    public function up(): void
    {
        DB::table('sysparam')
            ->whereIn('alias', [
                'level_warn',
                'level_mute',
                'level_kick',
                'level_announcement',
                'level_ban',
                'level_banip',
                'level_freeze',
            ])
            ->delete();
    }

    /**
     * 方法功能：回滚时恢复旧等级权限参数默认值。
     */
    public function down(): void
    {
        $now = now();

        foreach ([
            'level_warn' => ['5', '警告所需等级'],
            'level_mute' => ['50', '禁言所需等级'],
            'level_kick' => ['60', '踢人所需等级'],
            'level_announcement' => ['60', '设置公告所需等级'],
            'level_ban' => ['80', '封号所需等级'],
            'level_banip' => ['90', '封IP所需等级'],
            'level_freeze' => ['14', '冻结账号所需等级'],
        ] as $alias => [$body, $guidetxt]) {
            DB::table('sysparam')->updateOrInsert(
                ['alias' => $alias],
                [
                    'body' => $body,
                    'guidetxt' => $guidetxt,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]
            );
        }
    }
};
