<?php

namespace App\Http\Controllers;

use App\Models\EvaluationDetail;
use App\Models\Evalutaion;
use App\Models\Land;
use App\Models\Rule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AssesmentFormController extends Controller
{
    public function index()
    {
        $lands = Land::select('id', 'name')->orderBy('created_at', 'desc')->get();
        return view('assesment-form.index', compact('lands'));
    }

    public function store(Request $request)
    {
        // Ambil semua nilai input
        $parameters = [
            1 => $request->input('1'),  // pH
            2 => $request->input('2'),  // Ketinggian Tempat
            3 => $request->input('3'),  // Ketersediaan Air
            4 => $request->input('4')   // Curah Hujan
        ];

        // Ambil rule untuk setiap parameter
        $rules = Rule::whereIn('indicator_id', array_keys($parameters))->get()->keyBy('indicator_id');

        // Hitung Certainty Factor (CF) untuk setiap parameter
        $cfValues = [];
        foreach ($parameters as $indicatorId => $value) {
            if (isset($rules[$indicatorId])) {
                $cfValues[$indicatorId] = $rules[$indicatorId]->mb - $rules[$indicatorId]->md;
            } else {
                toast("Rule untuk indikator ID {$indicatorId} tidak ditemukan.", 'error')->position('top')->autoclose(3000);
                return redirect()->back();
            }
        }

        // Simpan ke database dalam satu transaksi
        DB::beginTransaction();
        try {
            // Simpan data evaluasi utama
            $evaluation = new Evalutaion;
            $evaluation->land_id = $request->land;
            $evaluation->save();

            // Simpan semua detail evaluasi
            foreach ($parameters as $indicatorId => $value) {
                EvaluationDetail::create([
                    'evaluation_id' => $evaluation->id,
                    'indicator_id' => $indicatorId,
                    'value' => $value,
                    'cf' => $cfValues[$indicatorId] ?? 0
                ]);
            }

            $result = "OKOKO";

            DB::commit();
            toast('Evaluasi berhasil disimpan!', 'success')->position('top')->autoclose(3000);
            return redirect()->back()->with("result", $result);
        } catch (\Throwable $th) {
            DB::rollBack();
            toast($th->getMessage(), 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }


    // public function store(Request $request)
    // {
    //     // dd($request->all());
    //     $pH = $request->input('1');
    //     $ketinggianTempat = $request->input('2');
    //     $ketersediaanAir = $request->input('3');
    //     $curahHujan = $request->input('4');

    //     $rulepH = Rule::where('indicator_id', 1)->where('range_min', '<=', $pH)->where('range_max', '>=', $pH)->first();
    //     $ruleKetinggianTempat = Rule::where('indicator_id', 2)->where('range_min', '<=', $ketinggianTempat)->where('range_max', '>=', $ketinggianTempat)->first();
    //     $ruleKetersediaanAir = Rule::where('indicator_id', 3)->where('range_min', '<=', $ketersediaanAir)->where('range_max', '>=', $ketersediaanAir)->first();
    //     $ruleCurahHujan = Rule::where('indicator_id', 4)->where('range_min', '<=', $curahHujan)->where('range_max', '>=', $curahHujan)->first();

    //     // dd([
    //     //     "pH" => $pH,
    //     //     "ketinggian_tempat" => $ketinggianTempat,
    //     //     "ketersediaan_air" => $ketersediaanAir,
    //     //     "curah_hujan" => $curahHujan,
    //     // ], [

    //     //     "rulepH" => $rulepH,
    //     //     "ruleKetinggianTempat" => $ruleKetinggianTempat,
    //     //     "ruleKetersediaanAir" => $ruleKetersediaanAir,
    //     //     "ruleCurahHujan" => $ruleCurahHujan
    //     // ]);

    //     $cfpH = $rulepH->mb - $rulepH->md;
    //     $cfketinggianTempat = $ruleKetinggianTempat->mb - $ruleKetinggianTempat->md;
    //     $cfketersediaanAir = $ruleKetersediaanAir->mb - $ruleKetersediaanAir->md;
    //     $cfcurahHujan = $ruleCurahHujan->mb - $ruleCurahHujan->md;

    //     // dd([
    //     //     "pH" => $pH,
    //     //     "ketinggian_tempat" => $ketinggianTempat,
    //     //     "ketersediaan_air" => $ketersediaanAir,
    //     //     "curah_hujan" => $curahHujan,
    //     // ], [

    //     //     "rulepH" => $rulepH,
    //     //     "ruleKetinggianTempat" => $ruleKetinggianTempat,
    //     //     "ruleKetersediaanAir" => $ruleKetersediaanAir,
    //     //     "ruleCurahHujan" => $ruleCurahHujan
    //     // ], [
    //     //     "pH" => $cfpH,
    //     //     "ketinggian_tempat" => $cfketinggianTempat,
    //     //     "ketersediaan_air" => $cfketersediaanAir,
    //     //     "curah_hujan" => $cfcurahHujan
    //     // ]);

    //     // $cfCombine1 = $cfpH + ($cfketinggianTempat * (1 - $cfpH));
    //     // $cfCombine2 = $cfCombine1 + ($cfketersediaanAir * (1 - $cfCombine1));
    //     // $cfCombine3 = $cfCombine2 + ($cfcurahHujan * (1 - $cfCombine2));

    //     // dd([
    //     //     "cfCombine(cfpH,cfketinggian)" => $cfCombine1,
    //     //     "cfCombine(cfOld, cfketersediaanAir)" => $cfCombine2,
    //     //     "cfCombine(cfOld, cfCurahHujan)" => $cfCombine3,
    //     // ]);

    //     // $presentaseKeyakinan = $cfCombine3 * 100;
    //     // dd($presentaseKeyakinan);
    //     // return view('assesment-form.index')->with('result', $presentaseKeyakinan);
    // }
}
