<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\QuizAttempt;
use App\Models\QuizAttemptAnswer;
use App\Models\Categories;
use App\Models\SubCategories;
use App\Models\BankQuestion;


class QuizController extends Controller
{

    public function submit(Request $request)
    {
        DB::beginTransaction();

        try {

            $attempt = QuizAttempt::create([
                'user_id' => auth()->id(),
                'category_id' => $request->category_id,
                'sub_category_id' => $request->sub_category_id,
                'total_questions' => $request->total_questions,
                'correct_answers' => $request->correct_answers,
                'wrong_answers' => $request->wrong_answers,
                'score' => $request->score,
                'started_at' => now(),
                'finished_at' => now()
            ]);

            foreach ($request->answers as $answer) {

                QuizAttemptAnswer::create([
                    'attempt_id' => $attempt->id,
                    'question_id' => $answer['question_id'],
                    'selected_answer' => $answer['selected_answer'],
                    'correct_answer' => $answer['correct_answer'],
                    'is_correct' => $answer['is_correct']
                ]);
            }

            DB::commit();

            return response()->json([
                'message' => 'Quiz submitted',
                'attempt_id' => $attempt->id
            ]);

        } catch (\Exception $e) {

            DB::rollBack();

            return response()->json([
                'message' => $e->getMessage()
            ], 500);
        }
    }

    public function getQuizCategories(Request $request)
    {
        $questionType = $request->question_type;

        $query = Categories::query();

        if ($questionType) {

            $query->whereExists(function ($subQuery) use ($questionType) {

                $subQuery->selectRaw('1')
                    ->from('bank_questions')
                    ->whereColumn(
                        'bank_questions.category_id',
                        'categories.id'
                    )
                    ->where(
                        'bank_questions.question_type',
                        $questionType
                    );
            });
        }

        return response()->json(
            $query
                ->with([
                    'subCategories' => function ($subQuery) use ($questionType) {

                        if ($questionType) {

                            $subQuery->whereExists(function ($exists) use ($questionType) {

                                $exists->selectRaw('1')
                                    ->from('bank_questions')
                                    ->whereColumn(
                                        'bank_questions.sub_category_id',
                                        'sub_categories.id'
                                    )
                                    ->where(
                                        'bank_questions.question_type',
                                        $questionType
                                    );
                            });

                        }

                    }
                ])
                ->orderBy('name')
                ->get()
        );
    }
}