<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller\view;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Role;
use App\Models\Makanan;
use App\Models\Rekomendasi;
use App\Http\Requests\UserRequest;
use App\Http\Requests\RoleRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Http\Requests\UpdateRoleRequest;
use Illuminate\Support\Facades\DB;



class AdminController extends Controller
{
    //
    public function admindash()
{
    $userCount = User::count();
    $makananCount = Makanan::count();

    // Ambil 5 rekomendasi tertinggi dengan relasi makanan
    $chartData = Rekomendasi::with('makanan')
        ->orderByDesc('nilai_akhir')
        ->take(5)
        ->get()
        ->map(function ($item) {
            return [
                'name' => $item->makanan->nama,
                'value' => $item->nilai_akhir
            ];
        });

    return view('admin.admindash', compact('userCount', 'makananCount', 'chartData'));
}


    // USER
    public function datauser(){
    $users = User::with('role')->get();  // Eager load the role relationship
    return view('admin.datauser.datauser', compact('users'));
}


    public function edituser(User $user){
        // Ambil semua role dari tabel roles
        $roles = Role::pluck('name', 'id'); // Mengambil nama dan id role
        return view('admin.datauser.edituser', compact('user', 'roles'));
    }
    public function tambahuser(){
        $roles = Role::all(); // Ambil data roles
        return view('admin.datauser.tambahuser', compact('roles'));
    }
    
    public function storeuser(UserRequest $request)
{
    // Validasi data
    $validatedData = $request->validated();

    // Simpan password tanpa hashing
    if ($request->filled('password')) {
        $validatedData['password'] = $request->input('password');  // Tidak melakukan hashing pada password
    }

    // Pastikan role_id adalah ID, bukan nama
    $validatedData['role_id'] = $request->input('role_id');  // Simpan role_id, bukan nama

    // Membuat user baru
    User::create($validatedData);
    return redirect()->route('datauser')->with('success', 'User added successfully!');
}

public function updateuser(UpdateUserRequest $request, User $user)
{
    // Validasi data
    $validatedData = $request->validated();

    // Simpan password tanpa hashing
    if ($request->filled('password')) {
        $validatedData['password'] = $request->input('password');  // Tidak melakukan hashing pada password
    }

    // Pastikan role_id adalah ID, bukan nama
    $validatedData['role_id'] = $request->input('role_id');  // Simpan role_id, bukan nama

    // Mengupdate user dengan data yang sudah divalidasi
    $user->update($validatedData);
    return redirect()->route('datauser');
}


    public function deleteuser(User $user) {
        $user->delete();
        return redirect()->route('datauser')->with('success', 'user deleted successfully');
    }




    // role
    public function role(){
        $roles = Role::all();
        return view('admin.role.role', compact('roles'));
    }
    public function editrole(Role $role){
        return view('admin.role.editrole', compact('role'));
    }
    public function tambahrole(){
        return view('admin.role.tambahrole');
    }
    public function storerole(RoleRequest $request){
        $validatedData = $request->validated();
        Role::create($validatedData);
        return redirect()->route('role');
    }
    public function updaterole(UpdateRoleRequest $request, Role $role){
        $validatedData = $request->validated();
        $role->update($validatedData);
        return redirect()->route('role');
    }
    public function deleterole(Role $role) {
        $role->delete();
        return redirect()->route('role')->with('success', 'user deleted successfully');
    }
    
}
