<?php

/**
 * 文件功能：后台游戏参数保存测试
 *
 * 覆盖游戏管理页保存参数时的关键写库行为，
 * 防止普通数值字段因为请求校验裁剪而出现“提示成功但未落库”的回归。
 */

namespace Tests\Feature\Feature;

use App\Models\GameConfig;
use App\Models\Room;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * 类功能：验证后台游戏配置保存链路。
 */
class AdminGameConfigControllerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * 方法功能：验证保存钓鱼冷却时间时会真正写入配置参数。
     */
    public function test_admin_can_update_fishing_cooldown_param(): void
    {
        $siteOwner = User::factory()->create([
            'id' => 1,
            'username' => 'site-owner',
            'user_level' => 100,
        ]);

        Room::query()->create([
            'id' => 1,
            'room_name' => 'test-room',
            'room_owner' => $siteOwner->username,
            'room_des' => '用于后台游戏配置测试',
            'room_time' => now(),
            'build_time' => now(),
        ]);

        $gameConfig = GameConfig::query()->create([
            'game_key' => 'fishing',
            'name' => '钓鱼',
            'icon' => 'fish',
            'description' => 'Fishing Game',
            'enabled' => true,
            'params' => [
                'room_scope_mode' => 'single',
                'room_ids' => [1],
                'fishing_cost' => 5,
                'fishing_wait_min' => 8,
                'fishing_wait_max' => 15,
                'fishing_cooldown' => 300,
            ],
        ]);

        $response = $this->actingAs($siteOwner)->post(route('admin.game-configs.params', $gameConfig), [
            'params' => [
                'room_scope_mode' => 'single',
                'room_ids' => [1],
                'fishing_cost' => 5,
                'fishing_wait_min' => 8,
                'fishing_wait_max' => 15,
                'fishing_cooldown' => 120,
            ],
        ]);

        $response->assertRedirect();
        $response->assertSessionHas('success');

        $this->assertSame(120, (int) ($gameConfig->fresh()->params['fishing_cooldown'] ?? 0));
    }

    /**
     * 方法功能：验证其他游戏的普通参数也会通过统一保存入口真正落库。
     */
    public function test_admin_can_update_multiple_game_params_via_shared_config_endpoint(): void
    {
        $siteOwner = User::factory()->create([
            'id' => 1,
            'username' => 'site-owner',
            'user_level' => 100,
        ]);

        $cases = [
            [
                'game_key' => 'baccarat',
                'name' => '百家乐',
                'icon' => 'dice',
                'params' => [
                    'room_scope_mode' => 'single',
                    'room_ids' => [1],
                    'interval_minutes' => 2,
                    'bet_window_seconds' => 60,
                ],
                'updated_key' => 'bet_window_seconds',
                'updated_value' => 90,
            ],
            [
                'game_key' => 'lottery',
                'name' => '双色球彩票',
                'icon' => 'lottery',
                'params' => [
                    'room_scope_mode' => 'single',
                    'room_ids' => [1],
                    'ticket_price' => 100,
                    'draw_hour' => 20,
                ],
                'updated_key' => 'ticket_price',
                'updated_value' => 188,
            ],
            [
                'game_key' => 'mystery_box',
                'name' => '神秘箱子',
                'icon' => 'box',
                'params' => [
                    'room_scope_mode' => 'single',
                    'room_ids' => [1],
                    'claim_window_seconds' => 60,
                    'normal_reward_min' => 500,
                ],
                'updated_key' => 'claim_window_seconds',
                'updated_value' => 75,
            ],
            [
                'game_key' => 'idiom',
                'name' => '猜谜活动',
                'icon' => 'puzzle',
                'params' => [
                    'room_scope_mode' => 'single',
                    'room_ids' => [1],
                    'reward_gold' => 50,
                    'reward_exp' => 30,
                ],
                'updated_key' => 'reward_gold',
                'updated_value' => 88,
            ],
        ];

        foreach ($cases as $index => $case) {
            $room = Room::query()->create([
                'room_name' => 'room-'.($index + 1),
                'room_owner' => $siteOwner->username,
                'room_des' => '用于后台游戏配置测试',
                'room_time' => now(),
                'build_time' => now(),
            ]);
            $roomId = (int) $room->id;

            $gameConfig = GameConfig::query()->create([
                'game_key' => $case['game_key'],
                'name' => $case['name'],
                'icon' => $case['icon'],
                'description' => 'shared config endpoint test',
                'enabled' => true,
                'params' => array_merge($case['params'], [
                    'room_ids' => [$roomId],
                ]),
            ]);

            $requestParams = $case['params'];
            $requestParams['room_ids'] = [$roomId];
            $requestParams[$case['updated_key']] = $case['updated_value'];

            $response = $this->actingAs($siteOwner)->post(route('admin.game-configs.params', $gameConfig), [
                'params' => $requestParams,
            ]);

            $response->assertRedirect();
            $response->assertSessionHas('success');

            $this->assertSame(
                $case['updated_value'],
                (int) ($gameConfig->fresh()->params[$case['updated_key']] ?? 0),
                "{$case['game_key']} 参数未写入数据库。",
            );
        }
    }
}
