<?php

namespace App\Console\Commands\Sync;

use App\Jobs\GrowingIO\TestingReportJob;
use App\Jobs\GrowingIO\UserInfoReportJob;
use App\Models\User\UserModel;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class GrowingIOUserGender extends Command
{
    protected $signature = 'sync:gio-user-gender';

    protected $description = 'Command description';

    public function handle()
    {
        $this->comment('total ' . UserModel::count());
        $count = 0;
        UserModel::orderBy('uid', 'asc')->chunk(99, function ($users) use (&$count) {
            /** @var Collection $users */
            $data = $users->map(function ($user) {
                /** @var UserModel $user */

                if ($user->sex === 1) {
                    $gender = '男性';
                } elseif ($user->sex === 2) {
                    $gender = '女性';
                } else {
                    $gender = '未知';
                }

                $uid = (string)$user->uid;

                dispatch_now(new TestingReportJob($uid));

                return [
                    'loginUserId' => $uid,
                    'gender' => $gender,
                ];
            })->all();


            $count += count($data);

            dispatch_now(new UserInfoReportJob($data));

            $this->comment('handled ' . $count);
        });
    }
}
