<?php

/**
 * 文件功能：后台等级经验阈值配置页测试
 *
 * 覆盖独立菜单页的展示、保存与非法阈值拦截，
 * 确保 levelexp 已从通用系统参数页中拆分出来单独维护。
 */

namespace Tests\Feature\Feature;

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

/**
 * 类功能：验证后台等级经验阈值独立配置页的核心行为。
 */
class AdminLevelExpConfigControllerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * 方法功能：验证独立配置页会按列表模式展示每一级阈值。
     */
    public function test_level_exp_index_displays_threshold_rows(): void
    {
        $this->seedLevelExpParam();
        $admin = $this->createSuperAdmin();

        $response = $this->actingAs($admin)->get(route('admin.level-exp-configs.index'));

        $response->assertOk();
        $response->assertSee('等级经验阈值管理');
        $response->assertSee('第 1 级');
        $response->assertSee('第 3 级');
        $response->assertSee('10');
        $response->assertSee('150');
        $response->assertSee('最高可配置到 99 级');
    }

    /**
     * 方法功能：验证独立配置页可保存新的等级经验阈值。
     */
    public function test_level_exp_update_persists_thresholds(): void
    {
        $this->seedLevelExpParam();
        $admin = $this->createSuperAdmin();

        $response = $this->actingAs($admin)->put(route('admin.level-exp-configs.update'), [
            'thresholds' => ['20', '80', '180', '360'],
        ]);

        $response->assertRedirect(route('admin.level-exp-configs.index'));
        $response->assertSessionHas('success');

        $this->assertDatabaseHas('sysparam', [
            'alias' => 'levelexp',
            'body' => '20,80,180,360',
        ]);
        $this->assertDatabaseHas('sysparam', [
            'alias' => 'maxlevel',
            'body' => '99',
        ]);
        $this->assertDatabaseHas('sysparam', [
            'alias' => 'superlevel',
            'body' => '100',
        ]);
    }

    /**
     * 方法功能：验证阈值必须严格递增，防止错误配置写入。
     */
    public function test_level_exp_update_requires_strictly_increasing_thresholds(): void
    {
        $this->seedLevelExpParam();
        $admin = $this->createSuperAdmin();

        $response = $this->from(route('admin.level-exp-configs.index'))
            ->actingAs($admin)
            ->put(route('admin.level-exp-configs.update'), [
                'thresholds' => ['20', '18', '100'],
            ]);

        $response->assertRedirect(route('admin.level-exp-configs.index'));
        $response->assertSessionHasErrors('thresholds');

        $this->assertDatabaseHas('sysparam', [
            'alias' => 'levelexp',
            'body' => '10,50,150',
        ]);
    }

    /**
     * 方法功能：验证等级数量不能超过用户最高可达等级。
     */
    public function test_level_exp_update_requires_threshold_count_not_exceed_maxlevel(): void
    {
        $this->seedLevelExpParam();
        Sysparam::updateOrCreate(
            ['alias' => 'maxlevel'],
            ['body' => '2', 'guidetxt' => '用户最高可达等级']
        );
        $admin = $this->createSuperAdmin();

        $response = $this->from(route('admin.level-exp-configs.index'))
            ->actingAs($admin)
            ->put(route('admin.level-exp-configs.update'), [
                'thresholds' => ['20', '80', '180'],
            ]);

        $response->assertRedirect(route('admin.level-exp-configs.index'));
        $response->assertSessionHasErrors('thresholds');

        $this->assertDatabaseHas('sysparam', [
            'alias' => 'levelexp',
            'body' => '10,50,150',
        ]);
        $this->assertDatabaseHas('sysparam', [
            'alias' => 'maxlevel',
            'body' => '2',
        ]);
        $this->assertDatabaseHas('sysparam', [
            'alias' => 'superlevel',
            'body' => '100',
        ]);
    }

    /**
     * 方法功能：创建可访问后台页面的超级管理员。
     */
    private function createSuperAdmin(): User
    {
        return User::factory()->create([
            'user_level' => 100,
        ]);
    }

    /**
     * 方法功能：初始化等级经验阈值参数。
     */
    private function seedLevelExpParam(): void
    {
        $rows = [
            'levelexp' => [
                'body' => '10,50,150',
                'guidetxt' => '按列表逐级维护升级所需的累计经验阈值',
            ],
            'maxlevel' => [
                'body' => '99',
                'guidetxt' => '用户最高可达等级',
            ],
            'superlevel' => [
                'body' => '100',
                'guidetxt' => '管理员级别（= 最高等级 + 1，拥有最高权限的等级阈值）',
            ],
        ];

        foreach ($rows as $alias => $payload) {
            Sysparam::updateOrCreate(
                ['alias' => $alias],
                $payload
            );
        }
    }
}
