<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Survey;
use App\Models\Response;
use App\Models\User;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index()
    {
        // count total borang
        $totalSurveys = Survey::count();

        // Count total respons
        $totalResponses = Response::count();

        // Count total users
        $totalUsers = User::count();

        // 5 recent borang
        $recentSurveys = Survey::with(['user', 'responses'])
            ->latest()
            ->take(5)
            ->get();

        // Recent Reviews (Ulasan) - "sikit je" (e.g., 5)
        $recentReviews = Survey::whereNotNull('ulasan')
            ->where('ulasan', '!=', '')
            ->with('user') // Assuming we might want to know who created the survey
            ->latest('updated_at')
            ->take(5)
            ->get();

        // Recent Users - "sikit je" (e.g., 5)
        $recentUsers = User::latest()
            ->take(5)
            ->get();

        return view('admin.dashboard', compact(
            'totalSurveys',
            'totalResponses',
            'totalUsers',
            'recentSurveys',
            'recentReviews',
            'recentUsers'
        ));
    }
}

