<?php

namespace App\Http\Controllers\Function;

use App\Models\Rule;
use App\Models\Statement;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LaborController extends Controller
{
    public function quest()
    {
        $quests = Rule::join('statements', 'rules.statement_id', '=', 'statements.id')
            ->orderBy('rules.rule_code', 'asc')
            ->select('statements.*')
            ->distinct()
            ->get();

        return view('employee.pages.theory.quest', compact('quests'));
    }

    public function calculator(Request $request)
    {
        // $quests = Rule::join('statements', 'rules.statement_id', '=', 'statements.id')
        //     ->orderBy('rules.rule_code', 'asc')
        //     ->select('statements.*')
        //     ->distinct()
        //     ->get();
        $quests = Statement::all();

        // Select all statement columns

        // Inisialisasi array untuk menyimpan ID pernyataan yang dijawab
        $answeredQuestIds = [];

        // Loop melalui setiap pernyataan untuk memeriksa jawaban yang diinput
        foreach ($quests as $quest) {
            // Dapatkan nilai input untuk pernyataan saat ini
            $inputValue = $request->input($quest->statement_code);

            //! tambahkan validasi jika jawaban kosong

            // Periksa apakah nilai input tidak kosong dan tidak sama dengan "on"
            if (!empty($inputValue) && $inputValue !== 'on') {
                // Jika dijawab, tambahkan ID pernyataan ke dalam array
                $answeredQuestIds[] = $quest->id;
            }
        }
        // dd($answeredQuestIds);
        // $evidence = ["65600f6d-aaa6-424f-b065-29e953cef651", "3351350f-0806-46b5-aebc-4fcf4ce08efb", "73fa4954-e2a1-4639-9e93-df54d85bb5c8", "38a5f7d8-bfba-4f53-9a53-428e52be162a"];

        $calc = new CalcController();

        $rulesQuery = $calc->getRulesData($answeredQuestIds);
        $rulesData = $calc->mapRulesData($rulesQuery);

        //? Mendapatkan data statement
        $statementData = $calc->getStatementData($rulesData);

        $personalitiesQuery = $calc->getPersonalitiesData();
        $personalitiesData = $calc->mapPersonalitiesData($personalitiesQuery);

        $result = $calc->processEvidence($rulesData, $personalitiesData);

        // dd($result);

        return view('employee.pages.theory.calc', [
            'rules' => $rulesData,
            'statementData' => $statementData,
            'personalitiesData' => $personalitiesData,
        ]);
    }
}
