<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Rekomendasi;
use App\Models\Makanan; // pastikan model di-import
use App\Models\JenisMakanan;
use App\Models\Kategori;
use Illuminate\Support\Facades\Auth; // pastikan ini ada di atas

class UserController extends Controller
{
    //
    public function userdash(){

        $totalData = DB::table('makanans')->count(); // atau model: DataMakanan::count()
        $totalKriteria = DB::table('kriterias')->count();
        $totalUser = DB::table('users')->count();
        return view('user.userdash', compact('totalData', 'totalKriteria', 'totalUser'));
    }
    public function userabout(){
        return view('user.userabout');
    }

    public function userfeature(){
        return view('user.userfeature');
    }

    public function userdata(Request $request)
{
    $jenisMakananList = JenisMakanan::all();
    $kategoriList = Kategori::all();

    $makanans = Makanan::query();

    if ($request->has('jenis_id') && $request->jenis_id != '') {
        $makanans->where('jenis_id', $request->jenis_id);
    }

    if ($request->has('kategori_id') && $request->kategori_id != '') {
        $makanans->where('kategori_id', $request->kategori_id);
    }

    $makanans = $makanans->get();

    return view('user.userdata', compact('makanans', 'jenisMakananList', 'kategoriList'));
}




public function userresult()
{
    $rekomendasi = Rekomendasi::with('makanan')
        ->where('nilai_akhir', '>', 0)
        ->orderByDesc('nilai_akhir')
        ->get();

    // Hitung total nilai akhir
    $totalNilaiAkhir = $rekomendasi->sum('nilai_akhir');

    return view('user.userresult', compact('rekomendasi', 'totalNilaiAkhir'));
}


public function show($id)
{
    // ambil data berdasarkan $id atau bisa juga switch/case
    return view('user.userfeature', compact('id'));
}


}
