<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Criteria;
use App\Models\SubCriteria;
use App\Models\Extracuricullar;
use App\Models\ExtracuricullarSubKriteria;
use RealRashid\SweetAlert\Facades\Alert;
use Illuminate\Support\Facades\Validator;

class PerhitunganController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function calculateBobotKriteria()
    {
        $criterias = Criteria::all();

        $totalKriteria = count($criterias);

        $bobotKriteria = [];

        foreach ($criterias as $key => $criteria) {
            $sum = 0;
            // Iterasi untuk setiap nilai prioritas pada kriteria
            for ($i = $key; $i < $totalKriteria; $i++) {
                // Hitung nilai SMARTER
                $sum += 1 / ($i + 1);
            }
            // Hitung bobot kriteria menggunakan rumus SMARTER ROC
            $bobot = $sum / $totalKriteria;

            $bobotKriteria[$criteria->nama] = $bobot;

            $criteria->update(['bobot' => $bobot]);
            $criteria->save();
    }

        return redirect()->route('kriteria.index')->with('success', 'Bobot kriteria berhasil dihitung dan disimpan.');
    }

    public function calculateBobotSubKriteria()
     {
        $criterias = Criteria::all();

        foreach ($criterias as $criteria) {
            // Ambil semua sub-kriteria untuk kriteria ini
            $subcriterias = SubCriteria::where('criteria_id', $criteria->id)->get();

            $totalSubKriteria = $subcriterias->count();

            $bobotSubKriteria = [];

             // Hitung bobot untuk setiap sub-kriteria
             foreach ($subcriterias as $key => $subcriteria) {
                $sum = 0;
                // Iterasi untuk setiap nilai prioritas pada kriteria
                for ($i = $key; $i < $totalSubKriteria; $i++) {
                    // Hitung nilai SMARTER
                    $sum += 1 / ($i + 1);
                }
                // Hitung bobot kriteria menggunakan rumus SMARTER ROC
                $bobot = $sum / $totalSubKriteria;

                $bobotSubKriteria[$subcriteria->nama] = $bobot;

                $subcriteria->update(['bobot' => $bobot]);
                $subcriteria->save();
             }
         }

         return redirect()->route('subkriteria.index')->with('success', 'Bobot sub kriteria berhasil dihitung dan disimpan.');
     }

    public function penilaian()
     {
        $criterias = Criteria::all();
        $extracuricullars = Extracuricullar ::all();
        $subcriterias = Subcriteria::all();

        return view('penilaian.input-nilai', compact('criterias','extracuricullars','subcriterias'));
     }

     public function transformasiNilai(Request $request)
    {
        $criterias = Criteria::all();
        $subcriterias = SubCriteria::all();
        $extracuricullars = Extracuricullar::all();

        return view('perhitungan.transformasi-nilai');
    }

     public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
