<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UserDashboardLayoutTest extends TestCase
{
    use RefreshDatabase;

    public function test_dashboard_renders_two_column_layout_for_desktop(): void
    {
        $user = \App\Models\User::create([
            'msnv'        => 5001,
            'name'        => 'System Admin',
            'mail'        => 'admin@runsystem.net',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_ADMIN'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        $this->actingAs($user);

        $response = $this->get('/my-page');

        $response->assertOk();
        $response->assertSee('Thanks gần đây', false);
        $response->assertSee('Bảng xếp hạng', false);
        $response->assertSee('lg:flex-row', false);
        $response->assertSee('flex-1 min-w-0', false);
        $response->assertSee('lg:w-[32%]', false);
        $response->assertSee('lg:min-w-[340px]', false);
        $response->assertSee('lg:max-w-[400px]', false);
    }

    public function test_dashboard_renders_ranking_list_from_database(): void
    {
        $user1 = \App\Models\User::create([
            'msnv'        => 5001,
            'name'        => 'User A',
            'mail'        => 'usera@runsystem.net',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        $user2 = \App\Models\User::create([
            'msnv'        => 5002,
            'name'        => 'User B',
            'mail'        => 'userb@runsystem.net',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[1],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        \App\Models\Administration::create([
            'msnv' => 5002,
            'received' => 0,
            'sender' => null,
            'sent' => 5,
            'receiver' => 5001,
            'date' => \Carbon\Carbon::now()->format('Y-m-d')
        ]);

        \App\Models\Administration::create([
            'msnv' => 5001,
            'received' => 5,
            'sender' => 5002,
            'sent' => 0,
            'receiver' => null,
            'date' => \Carbon\Carbon::now()->format('Y-m-d')
        ]);

        $this->actingAs($user1);

        $response = $this->get('/my-page');

        $response->assertOk();
        $response->assertSee('User A', false);
        $response->assertSee('User B', false);
    }

    public function test_dashboard_ranking_empty_state_when_no_data(): void
    {
        $user = \App\Models\User::create([
            'msnv'        => 5001,
            'name'        => 'User A',
            'mail'        => 'usera@runsystem.net',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        $this->actingAs($user);

        $response = $this->get('/my-page');
        $response->assertOk();
        $response->assertSee('Chưa có xếp hạng cho tháng này', false);
    }

    public function test_dashboard_ranking_dense_rank_and_deterministic_order(): void
    {
        $users = [];
        for ($i = 1; $i <= 5; $i++) {
            $char = chr(64 + $i);
            $users[$i] = \App\Models\User::create([
                'msnv'        => 5000 + $i,
                'name'        => "User {$char}",
                'mail'        => "user{$char}@runsystem.net",
                'pass'        => md5('password'),
                'departments' => config('constants.DEPARTMENTS')[0],
                'role'        => config('constants.ROLE_MEMBER'),
                'status'      => config('constants.STATUS_ACTIVE'),
                'card'        => 10,
                'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
                'first_login' => config('constants.FIRST_LOGIN_FALSE'),
            ]);

            \App\Models\Administration::create([
                'msnv' => 5000 + $i,
                'received' => 9,
                'sender' => 9999,
                'sent' => 0,
                'receiver' => null,
                'date' => \Carbon\Carbon::now()->format('Y-m-d')
            ]);
        }

        $this->actingAs($users[5]);

        $response = $this->get('/my-page');
        $response->assertOk();

        $response->assertSee('User A', false);
        $response->assertSee('User B', false);
        $response->assertSee('User C', false);
        $response->assertSee('User D', false);
        $response->assertSee('User E', false);
    }

    public function test_dashboard_ranking_inactive_users_excluded(): void
    {
        $activeUser = \App\Models\User::create([
            'msnv'        => 5001,
            'name'        => 'Active User',
            'mail'        => 'active@runsystem.net',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        $inactiveUser = \App\Models\User::create([
            'msnv'        => 5002,
            'name'        => 'Inactive User',
            'mail'        => 'inactive@runsystem.net',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_INACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        \App\Models\Administration::create([
            'msnv' => 5001,
            'received' => 10,
            'sender' => 9999,
            'sent' => 0,
            'receiver' => null,
            'date' => \Carbon\Carbon::now()->format('Y-m-d')
        ]);

        \App\Models\Administration::create([
            'msnv' => 5002,
            'received' => 20,
            'sender' => 9999,
            'sent' => 0,
            'receiver' => null,
            'date' => \Carbon\Carbon::now()->format('Y-m-d')
        ]);

        $this->actingAs($activeUser);

        $response = $this->get('/my-page');
        $response->assertOk();

        $response->assertSee('Active User', false);
        $response->assertDontSee('Inactive User', false);
    }

    public function test_dashboard_ranking_current_user_always_shown_at_bottom(): void
    {
        $user = \App\Models\User::create([
            'msnv'        => 5001,
            'name'        => 'User A',
            'mail'        => 'usera@runsystem.net',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        \App\Models\Administration::create([
            'msnv' => 5001,
            'received' => 10,
            'sender' => 9999,
            'sent' => 0,
            'receiver' => null,
            'date' => \Carbon\Carbon::now()->format('Y-m-d')
        ]);

        $this->actingAs($user);

        $response = $this->get('/my-page');
        $response->assertOk();

        $this->assertGreaterThan(1, substr_count($response->getContent(), 'User A'));
    }
}
