<?php

namespace Tests\Feature;

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

/**
 * 功能说明：验证排行榜与个人积分流水页面的访问和筛选行为。
 */
class LeaderboardControllerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * 验证用户可以访问排行榜首页并看到过滤后的榜单数据。
     */
    public function test_can_view_leaderboard_index(): void
    {
        $user = User::factory()->create(['exp_num' => 10, 'jjb' => 100, 'meili' => 5]);

        Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
        Sysparam::updateOrCreate(['alias' => 'leaderboard_limit'], ['body' => '20']);

        // Create users for leaderboard
        User::factory()->create(['user_level' => 10, 'exp_num' => 100, 'jjb' => 1000, 'meili' => 50]);
        User::factory()->create(['user_level' => 5, 'exp_num' => 50, 'jjb' => 500, 'meili' => 20]);

        // Super admin should be hidden
        User::factory()->create(['user_level' => 100, 'exp_num' => 10000, 'jjb' => 100000, 'meili' => 5000]);

        $response = $this->actingAs($user)->get(route('leaderboard.index'));

        $response->assertStatus(200);
        $response->assertViewIs('leaderboard.index');

        $topLevels = $response->viewData('topLevels');
        $this->assertNotNull($topLevels);
        $this->assertEquals(3, $topLevels->count()); // Excludes super admin
        $this->assertEquals(10, $topLevels->first()->user_level);

        $topExp = $response->viewData('topExp');
        $this->assertNotNull($topExp);
        $this->assertEquals(3, $topExp->count());
        $this->assertEquals(100, $topExp->first()->exp_num);

        $topWealth = $response->viewData('topWealth');
        $this->assertNotNull($topWealth);
        $this->assertEquals(3, $topWealth->count());
        $this->assertEquals(1000, $topWealth->first()->jjb);

        $topCharm = $response->viewData('topCharm');
        $this->assertNotNull($topCharm);
        $this->assertEquals(3, $topCharm->count());
        $this->assertEquals(50, $topCharm->first()->meili);
    }

    /**
     * 验证用户可以访问今日排行榜页面。
     */
    public function test_can_view_today_leaderboard(): void
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)->get(route('leaderboard.today'));

        $response->assertStatus(200);
        $response->assertViewIs('leaderboard.today');
    }

    /**
     * 验证用户可以访问自己的积分流水页面。
     */
    public function test_can_view_my_currency_logs(): void
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)->get(route('currency.my-logs'));

        $response->assertStatus(200);
        $response->assertViewIs('leaderboard.my-logs');
    }

    /**
     * 验证个人积分流水可以按收入和支出 tab 分开筛选。
     */
    public function test_can_filter_my_currency_logs_by_direction(): void
    {
        $user = User::factory()->create();

        UserCurrencyLog::query()->create([
            'user_id' => $user->id,
            'username' => $user->username,
            'currency' => 'gold',
            'amount' => 100,
            'balance_after' => 100,
            'source' => 'admin_adjust',
            'remark' => '测试收入',
            'created_at' => now(),
        ]);

        UserCurrencyLog::query()->create([
            'user_id' => $user->id,
            'username' => $user->username,
            'currency' => 'gold',
            'amount' => -30,
            'balance_after' => 70,
            'source' => 'admin_adjust',
            'remark' => '测试支出',
            'created_at' => now(),
        ]);

        $incomeResponse = $this->actingAs($user)->get(route('currency.my-logs', ['direction' => 'income']));

        $incomeResponse->assertStatus(200);
        $this->assertSame([100], $incomeResponse->viewData('logs')->pluck('amount')->all());
        $this->assertSame('income', $incomeResponse->viewData('direction'));

        $expenseResponse = $this->actingAs($user)->get(route('currency.my-logs', ['direction' => 'expense']));

        $expenseResponse->assertStatus(200);
        $this->assertSame([-30], $expenseResponse->viewData('logs')->pluck('amount')->all());
        $this->assertSame('expense', $expenseResponse->viewData('direction'));
    }

    /**
     * 验证个人积分流水可以按多个来源叠加筛选。
     */
    public function test_can_filter_my_currency_logs_by_multiple_sources(): void
    {
        $user = User::factory()->create();

        foreach ([CurrencySource::AUTO_SAVE, CurrencySource::SIGN_IN, CurrencySource::ADMIN_ADJUST] as $index => $source) {
            UserCurrencyLog::query()->create([
                'user_id' => $user->id,
                'username' => $user->username,
                'currency' => 'gold',
                'amount' => 10 + $index,
                'balance_after' => 100 + $index,
                'source' => $source->value,
                'remark' => $source->label(),
                'created_at' => now()->subMinutes($index),
            ]);
        }

        $response = $this->actingAs($user)->get(route('currency.my-logs', [
            'sources' => [
                CurrencySource::AUTO_SAVE->value,
                CurrencySource::SIGN_IN->value,
            ],
        ]));

        $response->assertStatus(200);
        $this->assertSame(
            [CurrencySource::AUTO_SAVE->value, CurrencySource::SIGN_IN->value],
            $response->viewData('logs')->pluck('source')->sort()->values()->all()
        );
        $this->assertSame(
            [CurrencySource::AUTO_SAVE->value, CurrencySource::SIGN_IN->value],
            $response->viewData('selectedSources')
        );
    }
}
