<?php

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Lib\Matrix;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class NetworkTest extends TestCase
{
    use RefreshDatabase;

    /**
     * @test
     */
    public function users_are_added_to_network_properly()
    {
        $user1 = User::factory()->create();
        $matrix = Matrix::set($user1);
        $user2 = User::factory()->create();
        $user3 = User::factory()->create();
        $user4 = User::factory()->create();
        $user5 = User::factory()->create();
        $user6 = User::factory()->create();

        $user2->ref_by = $user1->id;
        $user3->ref_by = $user1->id;
        $user4->ref_by = $user1->id;
        $user5->ref_by = $user1->id;
        $user6->ref_by = $user1->id;

        $user2->save();
        $user3->save();
        $user4->save();
        $user5->save();
        $user6->save();

        $matrix->setMatrix($user2);
        $matrix->addUserToNetwork();
        $matrix->setMatrix($user3);
        $matrix->addUserToNetwork();
        $matrix->setMatrix($user4);
        $matrix->addUserToNetwork();
        $matrix->setMatrix($user5);
        $matrix->addUserToNetwork();
        $matrix->setMatrix($user6);
        $matrix->addUserToNetwork();

        $this->assertCount(4, $user1->lowers);
        $this->assertCount(1, $user2->lowers);
        $this->assertEquals($user6->id, $user2->lowers->first()->id);
    }
}
