<?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 = Indicator::with('rules')->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',

            'parameter_type.required' => 'Jenis parameter wajib diisi',
            'parameter_type.string' => 'Jenis parameter harus berupa teks',
            'parameter_type.max' => 'Jenis parameter maksimal 25 karakter',

            'description.required' => 'Deskripsi wajib diisi',
            'description.string' => 'Deskripsi harus berupa teks',
            'description.max' => 'Deskripsi maksimal 40 karakter',
        ];

        $validator = Validator::make($request->all(), [
            'indicator' => 'required|exists:indicators,id',
            'parameter_type' => 'required|string|max:25',
            'description' => 'required|string|max:40',
            'cf' => 'required|numeric|min:-8|max:1',
        ], $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->parameter_type = $request->parameter_type;
        $rule->description = $request->description;
        $rule->cf = $request->cf;

        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)
    {
        // dd($request->all());
        $customMessage = [
            'parameter_type.required' => 'Jenis parameter wajib diisi',
            'parameter_type.string' => 'Jenis parameter harus berupa teks',
            'parameter_type.max' => 'Jenis parameter maksimal 25 karakter',

            'description.required' => 'Deskripsi wajib diisi',
            'description.string' => 'Deskripsi harus berupa teks',
            'description.max' => 'Deskripsi maksimal 40 karakter',

            'cf.required' => 'Nilai CF wajib diisi',
            'cf.numeric' => 'Nilai CF harus berupa angka',
            'cf.max' => 'Nilai CF maksimal 1',
        ];

        $validator = Validator::make($request->all(), [
            'parameter_type' => 'required|string|max:25',
            'description' => 'required|string|max:40',
            'cf' => 'required|numeric|max:1',
        ], $customMessage);

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

        $rule = Rule::find($id);
        $rule->parameter_type = $request->parameter_type;
        $rule->description = $request->description;
        $rule->cf = $request->cf;

        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();
        }
    }
}
