<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Kriteria;
use App\Models\Alternatif;
use App\Models\PerbandinganKriteria;
use App\Models\BobotKriteria;
use App\Models\Makanan;
use Illuminate\Support\Facades\Session;

class ProsesController extends Controller
{
    public function showPerbandingan()
    {
        $kriterias = Kriteria::all();
        return view('admin.proses.perbandingan', compact('kriterias'));
    }

    public function prosesSementara(Request $request)
    {
        $request->validate([
            'nilai' => 'required|array',
        ]);

        $kriterias = Kriteria::all();
        $nilai = [];

        // Hapus data lama (opsional, jika ingin reset)
        foreach ($kriterias as $baris) {
            foreach ($kriterias as $kolom) {
                if ($baris->id != $kolom->id) {
                    PerbandinganKriteria::where([
                        ['kriteria_id_1', $baris->id],
                        ['kriteria_id_2', $kolom->id]
                    ])->delete();
                }
            }
        }

        // Membuat matriks perbandingan dan menyimpannya ke session dan DB
        foreach ($kriterias as $baris) {
            foreach ($kriterias as $kolom) {
                if ($baris->id == $kolom->id) {
                    $nilai[$baris->id][$kolom->id] = 1;
                } elseif (isset($request->nilai[$baris->id][$kolom->id])) {
                    $val = floatval($request->nilai[$baris->id][$kolom->id]);
                    $nilai[$baris->id][$kolom->id] = $val;
                    $nilai[$kolom->id][$baris->id] = 1 / $val;

                    PerbandinganKriteria::updateOrCreate(
                        [
                            'kriteria_id_1' => $baris->id,
                            'kriteria_id_2' => $kolom->id
                        ],
                        [
                            'nilai' => $val
                        ]
                    );
                }
            }
        }

        Session::put('matriks', $nilai);
        return redirect()->route('perbandingan');
    }

    public function simpanPerbandingan()
    {
        $matriks = Session::get('matriks');

        if (!$matriks) {
            return redirect()->route('perbandingan')->with('error', 'Belum ada data untuk disimpan.');
        }

        Session::put('matriks_perbandingan', $matriks);
        return redirect()->route('hasil.normalisasi')->with('success', 'Data berhasil disimpan!');
    }

    public function hasilNormalisasi()
    {
        $kriterias = Kriteria::all();
        $matriks = Session::get('matriks_perbandingan');

        if (!$matriks) {
            return redirect()->route('perbandingan')->with('error', 'Data perbandingan belum tersedia.');
        }

        // Hitung jumlah per kolom
        $jumlahKolom = [];
        foreach ($kriterias as $kriteria) {
            $id = $kriteria->id;
            $jumlahKolom[$id] = 0;
            foreach ($matriks as $baris) {
                $jumlahKolom[$id] += $baris[$id];
            }
        }

        // Normalisasi
        $normalisasi = [];
        foreach ($kriterias as $baris) {
            $row = [];
            foreach ($kriterias as $kolom) {
                $row[] = $matriks[$baris->id][$kolom->id] / $jumlahKolom[$kolom->id];
            }
            $normalisasi[] = $row;
        }

        // Hitung bobot
        $bobot = [];
        foreach ($normalisasi as $baris) {
            $bobot[] = array_sum($baris) / count($baris);
        }

        // Simpan bobot ke database dan session
        foreach ($bobot as $index => $value) {
            BobotKriteria::updateOrCreate(
                ['kriteria_id' => $kriterias[$index]->id],
                ['bobot' => $value]
            );
        }

        Session::put('bobot_kriteria', $bobot);

        return view('admin.proses.normalisasi', [
            'kriterias' => $kriterias,
            'normalisasi' => $normalisasi,
            'bobot' => $bobot
        ]);
    }

    public function perbandinganAlternatif()
    {
        $kriterias = Kriteria::all();
        $alternatifs = Makanan::all();
        $bobot = Session::get('bobot_kriteria');

        if (!$bobot) {
            return redirect()->route('hasil.normalisasi')->with('error', 'Bobot kriteria belum tersedia.');
        }

        return view('admin.alternatif.perbandingan', compact('kriterias', 'alternatifs', 'bobot'));
    }
}
