<?php

namespace App\Http\Controllers\Api;

use App\Models\Answer;
use App\Models\Module;
use App\Models\Question;
use App\Models\UserAnswer;
use App\Models\UserCourse;
use App\User;
use Exception;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;


use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class QuestionController extends BaseController
{

    // 所有测试题目的列表
    public function preTestList(Request $request) {
        $data = Question::where('unit_id', $request->unit_id)->orderBy('order', 'desc')->with('answers')->get();
        return $this->response($data);
    }

    // 创建
    public function createQuestion(Request $request) {

        DB::transaction(function () use ($request){

            $data = Question::create([
                'name' => $request->input('name'),
                'type' => $request->input('type'),
                'unit_id' => $request->input('unit_id'), //如果不传unit_id表示是pre-test的问题
                'subject_id' => $request->input('subject_id'),
                'order' => $request->input('order'),
                'correct_answer' => $request->input('correct_answer')
            ]);

            foreach($request->input('answers') as $answer ) {
                Answer::create([
                    'name' => $answer['name'],
                    'label' => $answer['label'],
                    'value' => $answer['value'],
                    'question_id' => $data->id
                ]);
            }
            return $this->response($data);
        });
    }


    // 修改
    public function updateQuestion(Request $request) {
        $question = Question::findOrFail($request->input('id'));
        DB::transaction(function () use ($question, $request){

            $data = $question->update([
                'name' => $request->input('name'),
                'type' => $request->input('type'),
                'unit_id' => $request->input('unit_id'),
                'subject_id' => $request->input('subject_id'),
                'order' => $request->input('order'),
                'correct_answer' => $request->input('correct_answer')
            ]);

            $answers = Answer::where('question_id', $question->id)->delete();

            foreach($request->input('answers') as $answer ) {
                Answer::create([
                    'name' => $answer['name'],
                    'label' => $answer['label'],
                    'value' => $answer['value'],
                    'question_id' => $question->id
                ]);
            }
            return $this->response($data);
        });
    }

    // 删除
    public function deleteQuestion(Request $request) {
        DB::transaction(function () use ($request){
            $model = Question::findOrFail($request->input('id'));
            $model->delete();

            Answer::where('question_id', $model->id)->delete();

            return $this->response($model);
        });
    }


    // 用户答题
    public function answerQuestion(Request $request) {
        DB::transaction(function () use ($request){
            $answers = $request->answers;
            $userId = Auth::user()->id;

            if($request->pre_test) {
                $unit_id = null;
                $module_id = null;
                $subject_id = null;
            } else {
                $question = Question::find($answers[0]['question_id']);
                $unit_id = $question->unit_id;
                $module_id = $question->unit->module->id;
                $subject_id = $question->unit->module->subject_id;
            }
            // 插入答案数据
            foreach($answers as $answer) {
                // 问题
                UserAnswer::create([
                    'user_id' => $userId,
                    'question_id' => $answer['question_id'],
                    'unit_id' => $unit_id,
                    'type' => $answer['type'],
                    'answer' => $answer['answer'],
                    'correct_answer' => $answer['correct_answer']
                ]);
            }

            // 计算分数
            if($request->type ==1) {
                $score = 0;
                $motivation_score = 0;
            } else {
                $score = 0;
                $motivation_score = 0;
            }
            UserCourse::create([
                'user_id' => $userId,
                'course_id' => $request->course_id,
                'unit_id' => $unit_id,
                'module_id' => $module_id,
                'subject_id' => $subject_id,
                'score' => $score,
                'motivation_score' => $motivation_score,
                'type' => $request->type
            ]);
        });
        return $this->response('添加成功');
    }
}

