<?php

namespace App\Http\Controllers\MasterData;

use App\Http\Controllers\Controller;
use App\Models\Indicator;
use App\Models\Rule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RuleController extends Controller
{
    public function index()
    {
        $rules = Rule::with('indicator')->orderBy('created_at', 'desc')->get();
        $indicators = Indicator::select('id', 'name')->get();
        return view('master-data.aturan.index', compact('rules', 'indicators'));
    }

    public function store(Request $request)
    {
        $customMessage = [
            'indicator.required' => 'Harap pilih indikator',
            'indicator.exists' => 'Indikator tidak ditemukan',

            'ideal_min.required' => 'Nilai minimum wajib diisi',
            'ideal_min.numeric' => 'Nilai minimum harus berupa angka',

            'ideal_max.required' => 'Nilai maksimum wajib diisi',
            'ideal_max.numeric' => 'Nilai maksimum harus berupa angka',

            'mb.required' => 'Nilai MB wajib diisi',
            'mb.numeric' => 'Nilai MB harus berupa angka',
        ];

        $validator = Validator::make($request->all(), [
            'indicator' => 'required|exists:indicators,id',
            'ideal_min' => 'required|numeric',
            'ideal_max' => 'required|numeric',
            'mb' => 'required|numeric',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $rule = new Rule();

        $rule->indicator_id = $request->indicator;
        $rule->ideal_min = $request->ideal_min;
        $rule->ideal_max = $request->ideal_max;
        $rule->mb = $request->mb;

        try {
            $rule->save();
            toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function update(Request $request, $id)
    {
        $customMessage = [
            'indicator.required' => 'Harap pilih indikator',
            'indicator.exists' => 'Indikator tidak ditemukan',

            'ideal_min.required' => 'Nilai minimum wajib diisi',
            'ideal_min.numeric' => 'Nilai minimum harus berupa angka',

            'ideal_max.required' => 'Nilai maksimum wajib diisi',
            'ideal_max.numeric' => 'Nilai maksimum harus berupa angka',

            'mb.required' => 'Nilai MB wajib diisi',
            'mb.numeric' => 'Nilai MB harus berupa angka',
        ];

        $validator = Validator::make($request->all(), [
            'indicator' => 'required|exists:indicators,id',
            'ideal_min' => 'required|numeric',
            'ideal_max' => 'required|numeric',
            'mb' => 'required|numeric',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $rule = Rule::find($id);
        $rule->ideal_min = $request->ideal_min;
        $rule->ideal_max = $request->ideal_max;
        $rule->mb = $request->mb;

        try {
            $rule->save();
            toast('Berhasil mengubah aturan', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function destroy($id)
    {
        $rule = Rule::find($id);
        try {
            $rule->delete();
            toast('Data berhasil dihapus', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }
}
