<?php

namespace App\Http\Controllers\Appearance;

use App\Exceptions\AlertException;
use App\Http\Controllers\Core\Auth;
use App\Models\Appearance\AppearanceVoteModel;
use App\Models\User\UserModel;
use App\Services\Appearance\RankService;
use App\Services\Appearance\VoteService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class VoteController extends Controller
{


    /**
     * 投票
     * @param  Request  $request
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function vote(Request $request)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'type' => 'required|in:partner,school',
            'value' => 'required',
        ]);
        $type = $request->input('type', 'partner');
        $value = $request->input('value');
        $mediaId = $request->input('media_id', '');

        // 第二票以后需要榜单手机号
        if (AppearanceVoteModel::where('uid', $uid)->first()) {
            if (!UserModel::where('uid', $uid)->value('phone')) {
                return response([
                    'code' => 201,
                    'message' => '请先绑定手机号再投票'
                ]);
            }
        }

        // 今天是否为本渠道投过票
        if (AppearanceVoteModel::where('uid', $uid)->where('media_id', $mediaId)->today()->first()) {
            $today = true;
        } else {
            $today = false;
        }

        // 投票
        $voteService = new VoteService();
        switch ($type) {
            case "partner":
                $voteService->votePartner($uid, $value, $mediaId);
                break;
            case "school":
                $voteService->voteSchool($uid, $value, $mediaId);
                break;
        }
        // 渠道+1
        if ($mediaId && !$today && ($voteService->pick_partner || $voteService->pick_school)) {
            \DB::table('pocket.kdgx_activity_enrolls')
                ->where('media_id', $mediaId)
                ->where('activity_id', 3)
                ->increment('pick');
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'pick_partner' => $voteService->pick_partner,
                'pick_school' => $voteService->pick_school,
            ]
        ]);
    }


    /**
     * 投票明细（最近100）
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function votes(Request $request)
    {
        $votes = AppearanceVoteModel::where('type', 1)
            ->when($request->filled('school'), function ($query) use ($request) {
                return $query->where('school', $request->school);
            })->orderBy('id', 'desc')
            ->limit(100)
            ->get();
        foreach ($votes as $vote) {
            $vote->user;
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $votes
        ]);
    }


    /**
     * 申请上架
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function apply(Request $request)
    {
        $uid = Auth::auth();
        if ((new RankService())->passive($uid, true)) {
            return response([
                'code' => 200,
                'message' => 'Success'
            ]);
        } else {
            return response([
                'code' => 400,
                'message' => '未满足上榜资格'
            ]);
        }
    }


    /**
     * 设置下榜
     * @param  Request  $request
     * @return mixed
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function updateApply(Request $request)
    {
        $uid = Auth::auth();
        $voteService = new VoteService();
        $voteService->unApply($uid);
        return response([
            'code' => 200,
            'message' => 'OK'
        ]);
    }
}
