<?php

namespace App\Http\Controllers;

use App\Models\AlternatifSolusi;
use App\Models\CaseStudy;
use App\Models\Choice;
use App\Models\JawabanAlternatif;
use App\Models\JawabanKuis;
use App\Models\Node;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\URL;

class QuizController extends Controller
{
    public function show($id)
    {
        $case = CaseStudy::findOrFail($id);

        $startNode = Node::where('case_study_id', $id)
            ->where('is_start', true)
            ->first();

        return view('user.quiz.quiz1', compact('case', 'startNode'));
    }

    public function alternatifSolusi($id, Request $request)
    {
        // dd($id);
        $sesi = $request->sesi;
        $caseId = $id;

        /*
        |--------------------------------------------------------------------------
        | AMBIL JAWABAN TERAKHIR USER
        |--------------------------------------------------------------------------
        */

        $mulai = JawabanKuis::where('user_id', auth()->user()->id)
            ->where('case_study_id', $caseId)
            ->where('sesi', $sesi)
            ->orderBy('id', 'asc')
            ->first();
        // dd($mulai);

        $selesai = JawabanKuis::where('user_id', auth()->user()->id)
            ->where('case_study_id', $caseId)
            ->where('sesi', $sesi)
            ->orderBy('id', 'desc')
            ->first();
        // dd($selesai);


        $alternatif = AlternatifSolusi::where(
            'case_study_id',
            $caseId
        )
            ->where('mulai', $mulai->choice_id)
            ->where('selesai', $selesai->choice_id)
            ->first();
        if (!$alternatif) {
            $alternatif = AlternatifSolusi::where(
                'case_study_id',
                $caseId
            )
                ->where('selesai', $selesai->choice_id)
                ->first();
        }

        $check_ukuran = JawabanKuis::where('user_id', auth()->user()->id)
            ->where('case_study_id', $caseId)
            ->where('sesi', $sesi)
            ->get()->count();

        $alur_pertama = "";
        $alur_terakhir = "";
        $alur = "";

        if ($check_ukuran > 3) {
            $alur_pertama = JawabanKuis::with('choice')->where('user_id', auth()->user()->id)
                ->where('case_study_id', $caseId)
                ->where('sesi', $sesi)
                ->orderBy('id', 'asc')
                ->first();
            $alur_terakhir = JawabanKuis::with('choice')->where('user_id', auth()->user()->id)
                ->where('case_study_id', $caseId)
                ->where('sesi', $sesi)
                ->orderBy('id', 'desc')
                ->limit(2)->get();

            $alur = $alur_pertama->choice->data . ', ' . $alur_terakhir->pluck('choice.data')->implode(', ');
        } else {
            $alur = JawabanKuis::with('choice')->where('user_id', auth()->user()->id)
                ->where('case_study_id', $caseId)
                ->where('sesi', $sesi)
                ->get();

            $alur = $alur
                ->pluck('choice.data')
                ->implode(', ');
        }

        $case = CaseStudy::findOrFail($caseId);

        // dd($alur);

        return view(
            'user.quiz.quiz2',
            compact(
                'alternatif',
                'caseId',
                'alur',
                'case',
                'sesi'
            )
        );
    }

    public function answer(Request $request)
    {
        $choice = Choice::findOrFail($request->choice_id);

        $nextNode = Node::find($choice->next_node_id);

        return response()->json([
            'choice' => $choice,
            'node' => $nextNode,
            'choices' => $nextNode
                ? $nextNode->choices
                : [],
        ]);
    }

    public function finalEvaluation(Request $request)
    {
        $check_sesi = JawabanKuis::where('user_id', auth()->user()->id)->where('case_study_id', $request->id)->orderBy('id', 'desc')->first();
        $sesi = "1";
        if ($check_sesi) {
            $sesi = $check_sesi->sesi + 1;
        }

        foreach ($request->answers as $key => $value) {
            // dump($value);
            $store = [
                "user_id" => auth()->user()->id,
                "case_study_id" => $request->id,
                "choice_id" => $value,
                "sesi" => $sesi,
            ];

            // dump($store);

            JawabanKuis::create($store);
        }
        // die();

        return response()->json([
            'redirect' => URL::to(
                '/quiz/alternatif-solusi/' . $request->id . "?sesi=" . $sesi

            )
        ]);
    }

    public function alternatifSubmit(Request $request)
    {
        $store = [
            "user_id" => auth()->user()->id,
            "case_study_id" => $request->caseId,
            "jawaban" => $request->jawaban,
            "skor" => $request->score,
            "sesi" => $request->sesi,
        ];
        $data = JawabanAlternatif::create($store);
        $respon = "";
        $icon = "";
        $alternatif = AlternatifSolusi::find($request->alternatif_id);
        if ($data->skor == 4) {
            $respon = $alternatif->feedback_benar;
            $icon = "success";
        } else {
            $respon = $alternatif->feedback_salah;
            $icon = "info";
        }

        return response()->json([
            'respon' => $respon,
            'icon'=> $icon,
            'data' => $data,
        ]);
    }
}
