<?php

namespace App\Http\Controllers\Gnight;

use App\Exceptions\AlertException;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Miniprogram\Auth;
use App\Models\Gnight\GetVoiceModel;
use App\Models\Gnight\LockInviteModel;
use App\Models\Gnight\UserModel;
use Illuminate\Support\Facades\DB;

class LockInvite extends Controller
{

    /**
     * @param  int  $get_voice_id
     * @return array
     * @throws AlertException
     * @api {get} /api/gnight/lockinvite/check 助力解锁
     * @apiName check
     * @apiGroup lockinvite
     *
     * @apiParam {int} $get_voice_id 助力链接id
     *
     * @apiSuccess {int} lock 当前解锁进度
     */
    public function check(int $get_voice_id)
    {
        try {
            $uid = Auth::auth();
            if (
                LockInviteModel::where([
                ['invite_uid', $uid],
                ['get_voice_id', $get_voice_id]
                ])->exists()
            ) {
                throw new AlertException("你已为TA助力", 103);
            }
            $getVoice = GetVoiceModel::findOrFail($get_voice_id);
            if ($getVoice->lock >= 3) {
                throw new AlertException("已经被解锁", 101);
            }
            if ($uid == $getVoice->uid) {
                throw new AlertException("不能给自己助力", 105);
            }
            if (
                LockInviteModel::where([
                ['invite_uid', $uid],
                ['created_at', '>', mktime(0, 0, 0)]
                ])->exists()
            ) {
                throw new AlertException("你今日助力次数已用完", 102);
            }
            DB::beginTransaction();
            try {
                DB::table('kdgx_gnight_lock_invite')->insert([
                    'created_at' => time(),
                    'get_voice_id' => $get_voice_id,
                    'invite_uid' => $uid
                ]);
                DB::table('kdgx_gnight_get_voice')->where('id', $get_voice_id)->increment('lock', 1);
                if ($getVoice->lock == 2) {
                    // 发送小程序通知
                    try {
                        $notive = new Notive();
                        $notive->lock($getVoice->uid);
                    } catch (\Exception $e) {
                    }
                }
                DB::commit();
                return array(
                    'code' => 200,
                    'message' => 'success',
                    'data' => [
                        'lock' => $getVoice->lock + 1
                    ],
                );
            } catch (\Exception $e) {
                DB::rollBack();
                throw $e;
            }
        } catch (\Exception $e) {
            throw new AlertException($e->getMessage(), intval($e->getCode()));
        }
    }

    /**
     * @param  int  $get_voice_id
     * @return array
     * @api {get} /api/gnight/lockinvite/invites 邀请页信息
     * @apiName invites
     * @apiGroup lockinvite
     *
     * @apiParam {int} get_voice_id 助力链接id
     *
     * @apiSuccess {int} get_voice_id 助力链接id
     * @apiSuccess {int} lock 解锁进度
     * @apiSuccess {string} headimgurl 解锁用户头像
     * @apiSuccess {array} invites 助力用户列表
     * @apiSuccess {int} invites.uid 助力用户id
     * @apiSuccess {int} invites.headimgurl 助力用户头像
     *
     */
    public function invites(int $get_voice_id)
    {
        $result = array(
            'uid' => 0,
            'get_voice_id' => $get_voice_id,
            'lock' => 0,
            'headimgurl' => '',
            'invites' => []
        );
        $getVoice = GetVoiceModel::findOrFail($get_voice_id);
        $result['uid'] = $getVoice->uid;
        $user = UserModel::findOrFail($getVoice->uid);
        $result['headimgurl'] = $user->headimgurl;
        $result['lock'] = $getVoice->lock;
        $invites = LockInviteModel::where('get_voice_id', $get_voice_id)->get();
        foreach ($invites as &$invite) {
            try {
                $user = UserModel::findOrFail($invite->invite_uid);
                array_push($result['invites'], [
                    'uid' => $user->uid,
                    'headimgurl' => $user->headimgurl
                ]);
            } catch (\Exception $e) {
                array_push($result['invites'], [
                    'uid' => 0,
                    'headimgurl' => ''
                ]);
                continue;
            }
        }
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $result
        );
    }
}
