<?php

namespace App\Http\Controllers\Goodnight;

use App\Exceptions\AlertException;
use App\Http\Controllers\Miniprogram\Auth;
use App\Services\Goodnight\InviteService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class InviteController extends Controller
{
    /**
     * 标记邀请某人
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\DBException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function invite(Request $request)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'invite_uid' => 'required'
        ]);
        $invite_uid = $request->get('invite_uid');
        if ($uid == $invite_uid) {
            throw new AlertException("不能邀请自己", 101);
        }
        $inveService = new InviteService();
        $inveService->invite($uid, $invite_uid);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    public function inviteList(Request $request)
    {
        $uid = Auth::auth();
        $page = $request->get('page') ?? 1;
        $inveService = new InviteService();
        $data = $inveService->invitelist($page, 20, $uid);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'page' => $page,
                'limit' => 20,
                'total' => $data['total'],
                'list' => $data['list']
            ]
        );
    }
}
