<?php

/**
 * 文件功能：后台金币/积分流水查询测试
 *
 * 覆盖后台流水页对来源多选筛选的查询行为。
 */

namespace Tests\Feature\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 AdminCurrencyLogControllerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * 验证后台流水页支持按多个来源同时筛选。
     */
    public function test_admin_can_filter_currency_logs_by_multiple_sources(): void
    {
        Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
        $admin = User::factory()->create(['user_level' => 100]);

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

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

        $response->assertOk();
        $response->assertViewIs('admin.currency-logs.index');
        $response->assertViewHas('selectedSources', [
            CurrencySource::AUTO_SAVE->value,
            CurrencySource::SIGN_IN->value,
        ]);
        $this->assertSame(
            [CurrencySource::AUTO_SAVE->value, CurrencySource::SIGN_IN->value],
            $response->viewData('logs')->getCollection()->pluck('source')->sort()->values()->all()
        );
    }
}
