<?php

namespace App\Console\Commands\Mock;

use App\Packages\Friend\Models\FriendInvite;
use App\Packages\User\Models\User;
use Illuminate\Console\Command;

class MockInviteCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mock:invite';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create user mock invites';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = User::repository()->getRegisteredUsers();

        if ( ! $users ) {
            $this->info('No registered users');
            return false;
        }

        $max_friends = (int) $this->ask('Max invites per user')?:10;

        $status_bar = $this->output->createProgressBar($users->count());
        $status_bar->start();

        foreach ($users as $user) {

            for ($i = 0; $i <= rand(0, $max_friends); $i++) {
                $exit = 0;
                $finded = false;
                do {
                    $random_user = $users->random();
                    if ( ! FriendInvite::checkInvite($user, $random_user)) {
                        $finded = true;
                    }
                    $exit++;
                } while (!$finded && $exit <= 5);

                FriendInvite::createInvite($user, $random_user);
            }

            $status_bar->advance();
        }

        $status_bar->finish();
        $this->info('');
    }
}
