<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use App\Models\Career;
use App\Models\Personality;
use App\Models\Result;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class ResultController extends Controller
{
    public function index(): View|RedirectResponse
    {
        // $personalityNames = ["Investigative", "Artistic", "Social", "Enterprising", "Conventional", "Realistic", "Unknown"];
        // $personalityValues = [78, 15, 15, 14, 23, 30, 2];

        //? Get user data
        $user = auth()->user();

        $result = Result::with(['user', 'personality'])->where('user_id', $user->id)
            ->orderBy('created_at', 'desc')
            ->first();

        // ? Check if result is null
        if ($result === null) {
            return redirect()->route('student.quisionnare.start')->with('error', 'Tidak ada hasil yang ditemukan. Silakan mulai kuesioner untuk mendapatkan hasil.');
        }

        if ($result) {
            $topPersonalityId = $result->personality->pluck('id')->first();
        }

        //? Convert String to array
        $personalityNames = unserialize($result->overall_personality);
        $personalityValues = unserialize($result->overall_score);

        //? Get top personality description
        // $topPersonalityDesc = Personality::where('id', $result->personality_id)->first();

        $potentialCareers = Career::with(['personalities', 'departments'])
            ->whereHas('personalities', function ($query) use ($topPersonalityId) {
                $query->where('personalities.id', $topPersonalityId);
            })
            ->whereHas('departments', function ($query) use ($user) {
                $query->where('departments.id', $user->student->department_id);
            })
            ->get();

        // dd($personalityNames, $personalityValues, $topPersonalityDesc);

        return view('student.pages.result.index', compact('personalityNames', 'personalityValues', 'result', 'potentialCareers'));
    }

    public function resultList(): View
    {
        $results = Result::with('user', 'personality')
            ->where('user_id', auth()->user()->id)
            ->orderBy('created_at', 'desc')
            ->get();

        return view('student.pages.result.list', compact('results'));
    }

    public function resultDetail(Result $result): View
    {
        //? Get user data
        $user = auth()->user();

        //? Convert String to array
        $personalityNames = unserialize($result->overall_personality);
        $personalityValues = unserialize($result->overall_score);

        //? Get top personality description
        $result = Result::with('personality')->where('id', $result->id)->first();

        if ($result) {
            $topPersonalityId = $result->personality->pluck('id')->first();
        }

        $potentialCareers = Career::with(['personalities', 'departments'])
            ->whereHas('personalities', function ($query) use ($topPersonalityId) {
                $query->where('personalities.id', $topPersonalityId);
            })
            ->whereHas('departments', function ($query) use ($user) {
                $query->where('departments.id', $user->student->department_id);
            })
            ->get();

        return view('student.pages.result.index', compact('personalityNames', 'personalityValues', 'result', 'potentialCareers'));
    }
}
