<?php

namespace Tests\Feature\Feature;

use App\Models\BinaryLedger;
use App\Models\Rank;
use App\Models\RankHistory;
use App\Models\User;
use App\Models\UserNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UserModuleCompletionTest extends TestCase
{
    use RefreshDatabase;

    private function createUser(): User
    {
        return User::query()->create([
            'member_code' => 'LW1000001',
            'referral_code' => 'LW2000001',
            'name' => 'Module User',
            'email' => 'module-user@example.com',
            'mobile' => '9100000701',
            'password' => 'password',
            'role' => 'user',
            'status' => 'active',
        ]);
    }

    public function test_user_account_page_renders_edit_forms(): void
    {
        $user = $this->createUser();

        $response = $this->actingAs($user)->get('/user/profile');

        $response->assertOk();
        $response->assertSee('Profile Information');
        $response->assertSee('Security');
        $response->assertSee('Save Profile');
    }

    public function test_user_rank_page_shows_progress_and_history(): void
    {
        $user = $this->createUser();
        $rank = Rank::query()->create([
            'code' => 'RANK1',
            'name' => 'Silver',
            'priority' => 1,
            'required_active_directs' => 1,
            'required_weak_leg_bv' => 100,
            'required_team_turnover' => 200,
            'reward_amount' => 0,
            'is_active' => true,
        ]);

        $user->current_rank_id = $rank->id;
        $user->save();

        BinaryLedger::query()->create([
            'beneficiary_user_id' => $user->id,
            'leg' => 'left',
            'ledger_type' => 'repurchase',
            'direction' => 'credit',
            'units' => 150,
        ]);
        BinaryLedger::query()->create([
            'beneficiary_user_id' => $user->id,
            'leg' => 'right',
            'ledger_type' => 'repurchase',
            'direction' => 'credit',
            'units' => 120,
        ]);

        RankHistory::query()->create([
            'user_id' => $user->id,
            'from_rank_id' => null,
            'to_rank_id' => $rank->id,
            'transition_type' => 'upgrade',
            'weak_leg_bv' => 120,
            'team_turnover' => 270,
            'active_directs' => 1,
            'run_date' => now()->toDateString(),
        ]);

        $response = $this->actingAs($user)->get('/user/rank');

        $response->assertOk();
        $response->assertSee('Rank Qualification Matrix');
        $response->assertSee('Rank History');
        $response->assertSee('Silver');
    }

    public function test_user_can_create_support_ticket(): void
    {
        $user = $this->createUser();

        $response = $this->actingAs($user)->post('/user/support', [
            'subject' => 'Wallet mismatch',
            'message' => 'Please verify yesterday wallet entry.',
            'priority' => 'high',
            'category' => 'wallet',
        ]);

        $response->assertRedirect();
        $this->assertDatabaseHas('crm_leads', [
            'source' => 'support_ticket',
            'mobile' => '9100000701',
            'status' => 'new',
        ]);
    }

    public function test_user_announcements_page_shows_notifications(): void
    {
        $user = $this->createUser();
        UserNotification::query()->create([
            'user_id' => $user->id,
            'type' => 'system',
            'title' => 'System Update',
            'message' => 'Your dashboard has been updated.',
        ]);

        $response = $this->actingAs($user)->get('/user/announcements');

        $response->assertOk();
        $response->assertSee('My Notifications');
        $response->assertSee('System Update');
    }

    public function test_user_tree_page_renders_advanced_modes(): void
    {
        $user = $this->createUser();

        $response = $this->actingAs($user)->get('/user/tree');

        $response->assertOk();
        $response->assertSee('Genealogy');
        $response->assertSee('Binary');
        $response->assertSee('Sponsor');
        $response->assertSee('Node Information');
    }
}
