<?php

namespace App\Http\Controllers;

use App\Models\Rule;
use App\Models\Gejala;
use App\Models\Penyakit;
use Illuminate\Http\Request;

class RuleController extends Controller
{
    // =========================
    // LIST DATA
    // =========================
public function index(Request $request)
{
    // jumlah data per halaman
    $perPage = $request->perPage ?? 5;

    $query = Rule::with(['gejala', 'penyakit']);

    // SEARCH
    if ($request->search) {

        $query->whereHas('gejala', function ($q) use ($request) {

            $q->where('nama', 'like', '%' . $request->search . '%');

        })->orWhereHas('penyakit', function ($q) use ($request) {

            $q->where('nama', 'like', '%' . $request->search . '%');

        });
    }

    // pagination
    $rules = $query->latest()
        ->paginate($perPage)
        ->withQueryString();

    return view('admin.rule.index', compact('rules', 'perPage'));
}

    // =========================
    // FORM CREATE
    // =========================
    public function create()
    {
        $gejalas = Gejala::all();
        $penyakits = Penyakit::all();

        return view('admin.rule.create', compact('gejalas', 'penyakits'));
    }

    // =========================
    // SIMPAN DATA
    // =========================
    public function store(Request $request)
    {
        // ✅ VALIDASI
        $request->validate([
            'gejala_id' => 'required',
            'penyakit_id' => 'required',
            'mb' => 'required|numeric|min:0|max:1',
            'md' => 'required|numeric|min:0|max:1',
        ]);

        // ❗ CEK LOGIKA CF
        if ($request->md > $request->mb) {
            return back()->with('error', 'MD tidak boleh lebih besar dari MB');
        }

        // ✅ AUTO GENERATE KODE
        $last = Rule::latest()->first();
        $kode = 'R' . str_pad(($last ? $last->id + 1 : 1), 2, '0', STR_PAD_LEFT);

        // ✅ HITUNG CF
        $cf = $request->mb - $request->md;

        // ✅ SIMPAN
        Rule::create([
            'kode' => $kode,
            'gejala_id' => $request->gejala_id,
            'penyakit_id' => $request->penyakit_id,
            'mb' => $request->mb,
            'md' => $request->md,
            'cf' => $cf
        ]);

        return redirect()->route('rule.index')
            ->with('success', 'Rule berhasil ditambahkan');
    }

    // =========================
    // FORM EDIT
    // =========================
    public function edit($id)
    {
        $rule = Rule::findOrFail($id);
        $gejalas = Gejala::all();
        $penyakits = Penyakit::all();

        return view('admin.rule.edit', compact('rule', 'gejalas', 'penyakits'));
    }

    // =========================
    // UPDATE DATA
    // =========================
    public function update(Request $request, $id)
    {
        // ✅ VALIDASI
        $request->validate([
            'gejala_id' => 'required',
            'penyakit_id' => 'required',
            'mb' => 'required|numeric|min:0|max:1',
            'md' => 'required|numeric|min:0|max:1',
        ]);

        if ($request->md > $request->mb) {
            return back()->with('error', 'MD tidak boleh lebih besar dari MB');
        }

        $rule = Rule::findOrFail($id);

        // ✅ HITUNG CF
        $cf = $request->mb - $request->md;

        $rule->update([
            'gejala_id' => $request->gejala_id,
            'penyakit_id' => $request->penyakit_id,
            'mb' => $request->mb,
            'md' => $request->md,
            'cf' => $cf
        ]);

        return redirect()->route('rule.index')
            ->with('success', 'Rule berhasil diupdate');
    }

    // =========================
    // DELETE
    // =========================
    public function destroy($id)
    {
        Rule::destroy($id);

        return back()->with('success', 'Rule berhasil dihapus');
    }
}