<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Models\User\InviteConfigModel;
use App\Services\User\LikeInviteQuestionService;
use Illuminate\Http\Request;

class LikeInviteQuestionController extends Controller
{
    /**
     * 我的录制->问题列表
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\VersionException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function createQuestion(Request $request)
    {
        $low_version = $latest_version = "2.11.10";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);

        $uid = Auth::auth();
        $this->validate($request, [
            'question' => 'required',
            'voice_src' => 'required',
            'template_id' => 'required',
            'question_type' => 'required'
        ]);

        $liqs = new LikeInviteQuestionService();
        $id = $liqs->createQuestion($uid, $request->toArray());

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => [
                'id' => $id
            ]
        ]);
    }

    /**
     * 回答心动考验->换题列表
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\VersionException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function questionList(Request $request)
    {
        $low_version = $latest_version = "2.11.10";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);

        Auth::auth();

        $this->validate($request, [
            'user_id' => 'required|integer',
            'question_type' => 'required|in:0,1'
        ]);

        $user_id = $request->post('user_id');
        $question_type = $request->post('question_type', 0);

        $liqs = new LikeInviteQuestionService();
        $data = $liqs->questionList($user_id, $question_type);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }

    /**
     * 我的录制->问题列表
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\VersionException
     */
    public function list(Request $request)
    {
        $low_version = $latest_version = "2.11.10";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);

        $uid = Auth::auth();

        $type = $request->get('type', 0);
        $page = $request->get('page', 1);

        $pages = array(
            'page' => $request->get('page') ?? 1,
            'limit' => 10
        );

        $liqs = new LikeInviteQuestionService();
        $data = $liqs->list($uid, $type, $pages);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }

    /**
     * 我的录制->删除问题
     * @param  int  $question_id
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\VersionException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function deleteQuestion(int $question_id, Request $request)
    {
        $low_version = $latest_version = "2.11.10";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);

        $uid = Auth::auth();

        $liqs = new LikeInviteQuestionService();
        $liqs->deleteQuestion($uid, $question_id);

        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 心动考验开关
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\VersionException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @deprecated 2.17.0 以上版本弃用
     * @version 2.17.0
     */
    public function likeInviteQuestionSwitch(Request $request)
    {
        $low_version = $latest_version = "2.17.0";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);

        $uid = Auth::auth();

        $this->validate($request, [
            'task_type' => "required|in:task_photo,task_question,task_sing",
            'switch' => "required|in:0,1"
        ]);

        $task_type = $request->post('task_type');
        $switch = $request->post('switch');

        $liqs = new LikeInviteQuestionService();

        $liqs->likeInviteQuestionSwitch($uid, $task_type, $switch);

        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 心动考验页的取消喜欢|喜欢 某人
     * @param  int  $question_id
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\VersionException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @version 2.11.10
     */
    public function thumbQuestion(int $question_id, Request $request)
    {
        $low_version = $latest_version = "2.11.10";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);

        $uid = Auth::auth();

        $liqs = new LikeInviteQuestionService();
        $data = $liqs->thumb($uid, $question_id);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }


    /**
     * 设置邀请问题
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function setInviteQuestion(Request $request)
    {
        $low_version = $latest_version = "2.17.0";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);
        $uid = Auth::auth();
        $this->validate($request, [
            'task_photo' => '',
            'task_question' => '',
            'task_sing' => '',
            'task_paint' => '',
            'task_question_data' => 'array',
            'task_sing_data' => 'array',
        ]);
        $invite_config = InviteConfigModel::updateOrCreate(
            ['uid' => $uid],
            $request->all()
        );
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $invite_config
        ]);
    }


    /**
     * 获取邀请的问题
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getInviteQuestion(Request $request)
    {
        $low_version = $latest_version = "2.17.0";
        version($request->header('version', '0.00.01'), $low_version, $latest_version);
        $uid = Auth::auth();
        $invite_config = InviteConfigModel::firstOrCreate([
            'uid' => $uid
        ]);
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $invite_config
        ]);
    }
}
