<?php

namespace Modules\User\Http\Controllers\Api\Rank;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\User\Models\User;
use Modules\User\Traits\RankDataTrait;

class IndexController extends Controller
{
    use RankDataTrait;

    /**
     * Notes: 总排行
     *
     * @Author: 玄尘
     * @Date: 2022/8/17 15:10
     * @param  Request  $request
     * @return JsonResponse
     */
    public function total(Request $request): JsonResponse
    {
        $dateBetween = [
            '2022-06-01 00:00:00',
            now()->toDateTimeString(),
        ];

        $vipUsers = $this->getRankDataTrait($dateBetween);
        $data     = [];
        $i        = 1;
        foreach ($vipUsers as $user) {
            $data[] = [
                'rank'     => $i,
                'avatar'   => $user->info->avatar,  //头像
                'nickname' => $user->info->nickname,
                'rate'     => '---',
                'number'   => $user->countChild,
            ];
        }
        return $this->success([
            'page_id' => 6,
            'rand'    => $data
        ]);
    }

    /**
     * Notes: 推荐用户总排行
     *
     * @Author: 玄尘
     * @Date: 2022/8/17 15:11
     * @return JsonResponse
     */
    public function totalUser(): JsonResponse
    {
        $user = Api::user();

        $users = User::query()
            ->withCount('children')
            ->take(10)
            ->latest('children_count')
            ->get()
            ->map(function ($info) use ($user) {
                return [
                    'user'  => [
                        'user_id'  => $info->id,
                        'username' => $info->username,
                        'nickname' => $info->info->nickname ?? '',
                        'avatar'   => $info->info->avatar ?? '',
                    ],
                    'is_my' => $info->id == $user->id,
                    'total' => $info->children_count,
                ];
            })
            ->pad(10, [
                'user'  => [
                    'user_id'  => 0,
                    'username' => '---',
                    'nickname' => '---',
                    'avatar'   => '---',
                ],
                'is_my' => false,
                'total' => 0,
            ]);

        $users = $users->toArray();

        $i      = 1;
        $top    = $other = [];
        $myRank = 0;
        foreach ($users as &$rank) {
            $rank['rank'] = $i;
            if ($rank['is_my']) {
                $myRank = $i;
            }

            if ($i < 4) {
                $top[] = $rank;
            } else {
                $other[] = $rank;
            }
            $i++;
        }

        $childrenCount = $user->children()->count();
        if (! $myRank) {
            $myRank = User::query()
                ->withCount('children')
                ->latest('children_count')
                ->where('children_count'.'>', $childrenCount)
                ->count();
        }

        $data = [
            'user'  => [
                'username' => $user->username,
                'nickname' => $user->info->nickname,
                'avatar'   => $user->info->avatar,
                'total'    => $user->sign->counts,
                'rank'     => $myRank ?: $myRank + 1,
            ],
            'top'   => $top,
            'other' => $other,
        ];

        return $this->success($data);
    }

}
