<?php

use App\Models\LotterySetting;
use Illuminate\Database\Migrations\Migration;

/**
 * 玩法规则 HTML：由单键 frontend.play_rules_html 拆分为 zh/en/ne 三语键。
 * 已有内容迁移到 frontend.play_rules_html_zh。
 */
return new class extends Migration
{
    private const LEGACY_KEY = 'frontend.play_rules_html';

    private const I18N_KEYS = [
        'frontend.play_rules_html_zh' => '玩家端玩法规则页 HTML（中文）',
        'frontend.play_rules_html_en' => '玩家端玩法规则页 HTML（English）',
        'frontend.play_rules_html_ne' => '玩家端玩法规则页 HTML（नेपाली）',
    ];

    public function up(): void
    {
        $legacyRow = LotterySetting::query()->where('setting_key', self::LEGACY_KEY)->first();
        $legacyValue = $legacyRow?->value_json;

        foreach (self::I18N_KEYS as $key => $description) {
            if (LotterySetting::query()->where('setting_key', $key)->exists()) {
                continue;
            }

            $value = '';
            if ($key === 'frontend.play_rules_html_zh' && is_string($legacyValue) && trim($legacyValue) !== '') {
                $value = $legacyValue;
            }

            LotterySetting::query()->create([
                'setting_key' => $key,
                'value_json' => $value,
                'group_name' => 'frontend',
                'description_zh' => $description,
            ]);
        }
    }

    public function down(): void
    {
        LotterySetting::query()
            ->whereIn('setting_key', array_keys(self::I18N_KEYS))
            ->delete();
    }
};
