<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Riwayat;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Pembelajaran;

class HomeController extends Controller
{
    public function index()
    {
        $users = User::query()->latest()
            ->where("role_id", "=", "9bae0006-2480-4f34-9856-cc605550b9b4")
            ->take(8)
            ->get();

        // Ambil riwayat membaca berdasarkan user yang sedang login
        $riwayat = Riwayat::where('user_id', Auth::id())->get();

        // Ambil pembelajaran yang terkait dengan user yang sedang login melalui tabel pivot
        $pembelajarans = Pembelajaran::whereHas('users', function ($query) {
            $query->where('user_id', Auth::id());
        })->get();

        return view('home', [
            "users" => $users,
            "riwayat" => $riwayat,
            "pembelajarans" => $pembelajarans,
        ]);
    }
    
    public function showAllStudent()
    {
        $users = User::query()->latest()
            ->where("role_id", "=", "9bae0006-2480-4f34-9856-cc605550b9b4")
            ->get();

        return view('home-students', [
            "users" => $users
        ]);
    }
}