<?php

namespace App\Http\Controllers;

use App\Models\Alternatif;
use App\Models\Penilaian;
use App\Models\Crips;
use App\Models\Kriteria;
use App\Http\Controllers\AlgoritmaController; // Impor AlgoritmaController
use Illuminate\Http\Request;

class PenilaianController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $penilaians = Penilaian::with(['alternatif', 'kriteria', 'crips'])->get();
        $kriteria = Kriteria::all();
        $alternatif = Alternatif::with('penilaian.crips')->get();
        return view('admin.penilaian.index', compact('penilaians', 'kriteria', 'alternatif'));
    }

    public function create()
    {
        $alternatifs = Alternatif::all();
        $kriteria = Kriteria::all();
        $crips = Crips::all();
        return view('penilaian.create', compact('alternatifs', 'kriteria', 'crips'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'alternatif_id' => 'required|exists:alternatif,id',
            'kriteria_id' => 'required|exists:kriteria,id',
        ]);

        // Ambil data alternatif berdasarkan id_alternatif
        $alternatif = Alternatif::find($validated['alternatif_id']);
        $kriteria = Kriteria::find($validated['kriteria_id']);

        // Tentukan nilai berdasarkan id_kriteria
        $nilai = null;
        switch ($kriteria->id) {
            case 1: $nilai = $alternatif->usia; break; // Usia
            case 2: $nilai = $alternatif->pendidikan; break; // Pendidikan
            case 3: $nilai = $alternatif->ipk; break; // IPK
            case 4: $nilai = $alternatif->jurusan; break; // Jurusan
            case 5: $nilai = $alternatif->lama_studi; break; // Lama Studi
            case 6: $nilai = $alternatif->pengalaman; break; // Pengalaman
            case 7: $nilai = $alternatif->serkom; break; // Serkom
        }

        // Panggil getCripsId untuk mendapatkan id_crips
        $crips_id = $this->getCripsId($kriteria->id, $nilai);

        if (!$crips_id) {
            return redirect()->back()->with('error', 'Sub-kriteria tidak ditemukan untuk kriteria ini.');
        }

        // Simpan data penilaian
        $penilaian = Penilaian::create([
            'alternatif_Id' => $validated['alternatif_id'],
            'kriteria_id' => $validated['kriteria_id'],
            'crips_id' => $crips_id,
        ]);

        // Panggil perhitungan SAW dari AlgoritmaController
        $algoritmaController = new AlgoritmaController();
        $algoritmaController->calculateSAW(
            Alternatif::with('penilaian.crips')->get(),
            Kriteria::get(),
            Penilaian::with('crips', 'alternatif')->get()
        );

        return redirect()->route('penilaian.index')->with('msg', 'Penilaian berhasil disimpan dan dihitung!');
    }

    private function getCripsId($kriteria_id, $nilai)
    {
        $crips = Crips::where('kriteria_id', $kriteria_id)->get();
        $crips_id = null;

        switch ($kriteria_id) {
            case 1: // Usia
                foreach ($crips as $crip) {
                    if ($crip->nama_crips == '23-24' && $nilai >= 23 & $nilai <= 24) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '25-26' && $nilai >= 25 && $nilai <= 26) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '27-28' && $nilai >= 27 && $nilai <= 28) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '29-30' && $nilai >= 29 && $nilai <= 30) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '> 30' && $nilai > 30) $crips_id = $crip->id;
                }
                 break;

            case 2: // Pendidikan
                foreach ($crips as $crip) {
                    if ($crip->nama_crips == $nilai) $crips_id = $crip->id;
                }
                break;

            case 3: // IPK
                foreach ($crips as $crip) {
                    if ($crip->nama_crips == '3.5-4.0' && $nilai >= 3.5 && $nilai <= 4.0) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '3.0-3.49' && $nilai >= 3.0 && $nilai < 3.5) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '2.75-2.99' && $nilai >= 2.75 && $nilai < 3.0) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '2.5-2.74' && $nilai >= 2.5 && $nilai < 2.75) $crips_id = $crip->id;
                }
                break;

                case 4: // Jurusan
                    foreach ($crips as $crip) {
                        if ($crip->nama_crips == 'Teknik Sipil' && $nilai == 'Teknik Sipil') $crips_id = $crip->id;
                        elseif ($crip->nama_crips == 'Arsitektur' && $nilai == 'Arsitektur') $crips_id = $crip->id;
                        elseif ($crip->nama_crips == 'Teknologi Rekayasa Konstruksi Bangunan Gedung' && $nilai == 'Teknologi Rekayasa Konstruksi Bangunan Gedung') $crips_id = $crip->id;
                        elseif ($crip->nama_crips == 'Akuntansi' && $nilai == 'Akuntansi') $crips_id = $crip->id;
                        elseif ($crip->nama_crips == 'Lainnya' && $nilai == 'Lainnya') $crips_id = $crips->id;
                    }
                    break;

            case 5: // Lama Studi
                foreach ($crips as $crip) {
                    if ($crip->nama_crips == '<4 tahun' && $nilai < 4) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '4-5 tahun' && $nilai >= 4 && $nilai <= 5) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '>5 tahun' && $nilai > 5) $crips_id = $crip->id;
                }
                break;

            case 6: // Pengalaman
                foreach ($crips as $crip) {
                    if ($crip->nama_crips == '1-3 tahun' && $nilai >= 1 && $nilai <= 3) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == '>3 tahun' && $nilai > 3) $crips_id = $crip->id;
                }
                break;

            case 7: // Serkom
                foreach ($crips as $crip) {
                    if ($crip->nama_crips == 'Punya' && $nilai == 1) $crips_id = $crip->id;
                    elseif ($crip->nama_crips == 'Tidak punya' && $nilai == 0) $crips_id = $crip->id;
                }
                break;
        }

        return $crips_id;
    }
}