<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Hasil;
use Illuminate\Support\Facades\Auth;

class HistoryController extends Controller
{
    public function index()
    {
        $userId = Auth::id();
        $hasilTerbaru = Hasil::where('id_user', $userId)
            ->orderBy('created_at', 'desc')
            ->first();

        $TOTAL_SOAL = 16;

        if ($hasilTerbaru) {
            $vark = [
                'Visual' => $hasilTerbaru->visual ?? 0,
                'Auditory' => $hasilTerbaru->auditory ?? 0,
                'Read/Write' => $hasilTerbaru->readwrite ?? 0,
                'Kinesthetic' => $hasilTerbaru->kinesthetic ?? 0,
            ];

            $jumlahJawaban = array_sum($vark);
            $persen = round(($jumlahJawaban / $TOTAL_SOAL) * 100);

            $max = max($vark);
            $tertinggi = array_keys($vark, $max);
            $gayaBelajar = count($tertinggi) > 1 ? 'Multimodal' : $tertinggi[0];
        } else {
            $hasilTerbaru = null;
            $persen = 0;
            $gayaBelajar = '-';
            $jumlahJawaban = 0;
        }

        return view('backend.history_quis', compact('hasilTerbaru', 'persen', 'gayaBelajar', 'TOTAL_SOAL', 'jumlahJawaban'));
    }

}

