<?php

namespace App\Http\Controllers\Fpdx;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Managers\SharePairManager;
use App\Models\Fpdx\PairModel;
use App\Models\Fpdx\ActivityModel;
use App\Models\Fpdx\InviteModel;
use App\Models\User\UserModel;
use App\Models\User\AuthKey;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;

class Invite extends Controller
{
    /**
     * 检验邀请
     * @param  int  $invite_id
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \Throwable
     */
    public function invite(int $invite_id, Request $request)
    {
        $platform = $request->header('platform');
        if ('qq' == $platform) {
            $this->validate($request, ['openid' => 'required']);
            $openid = $request->get('openid');
        } else {
            $uid = Auth::auth();
            $auth = AuthKey::where(array(['uid', $uid], ['auth_type', config("miniprogram.public_id")]))->firstOrFail();
            $openid = $auth->auth_key;
        }
        $ps = new SharePairManager();
        $data = $ps->check($openid, $invite_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 会员助力提升成功率
     * @param  int  $invite_id
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \Throwable
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @deprecated Share/PairVipCheck
     */
    public function vipCheck(int $invite_id)
    {
        $uid = Auth::auth();
        $ps = new SharePairManager();
        $data = $ps->vipCheck($uid, $invite_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 邀请进度
     * @param  int  $invite_id
     * @return float(2, 2)
     */
    public function progressInvite(int $invite_id)
    {
        $dis_score = InviteModel::where([
            ['id', $invite_id],
            ['state', '!=', 2]
        ])->sum('dis_score');
        return sprintf('%.2f', $dis_score / 100);
    }

    /**
     * 获取邀请信息
     * @param  int  $invite_id
     * @return array
     */
    public function get(int $invite_id)
    {
        $pair = PairModel::find($invite_id);
        if (collect($pair)->isEmpty()) {
            return array(
                'code' => 101,
                'message' => '参数错误'
            );
        } else {
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => $pair
            );
        }
    }

    /**
     * @return array
     * @throws \ApiException
     * @api 获取最近三个人的报名气泡
     * @deprecated Invite/LastInvite
     */
    public function lastInvite()
    {
        try {
            $ings = ActivityModel::getActivitys();
            $invites = InviteModel::where([
                ['stage_id', $ings['next']],
                ['state', '!=', 2]
            ])->take(30)->groupBy('pair_id')->get(['uid', 'pair_id']);
            foreach ($invites as &$invite) {
                $invite->user = UserModel::find(
                    $invite->uid,
                    ['uid', 'headimgurl', 'nickname', 'sex', 'address', 'home']
                );
                $refund = InviteModel::where([
                    ['state', 1],
                    ['pair_id', $invite->pair_id]
                ])->count();
                if ($refund > 0) {
                    $invite->invite = array(
                        'type' => 1,
                        'count' => $refund,
                        'dis_count' => InviteModel::where([
                            ['state', 1],
                            ['pair_id', $invite->pair_id]
                        ])->sum('dis_score')
                    );
                } else {
                    $invite->invite = array(
                        'type' => 0,
                        'count' => InviteModel::where([
                            ['state', 0],
                            ['pair_id', $invite->pair_id]
                        ])->count(),
                        'dis_score' => InviteModel::where([
                            ['state', 0],
                            ['pair_id', $invite->pair_id]
                        ])->sum('dis_score')
                    );
                }
            }
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => $invites
            );
        } catch (\Exception $e) {
            dump($e);
            throw new \ApiException($e->getMessage(), $e->getCode());
        }
    }
}
