<?php

namespace Tests\Feature;

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

class LeaderboardControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_can_view_leaderboard_index()
    {
        $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()
    {
        $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()
    {
        $user = User::factory()->create();

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

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