<?php

// app/Http/Controllers/ReviewController.php

namespace App\Http\Controllers;

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

class ReviewController extends Controller
{
    // 🔥 Landing Page View
    public function welcome()
    {
        $totalUsers = User::count();
        $reviews = Review::with('user')->latest()->take(6)->get();

        return view('welcome', compact('totalUsers', 'reviews'));
    }

    // ✅ Reviews List Page (if needed)
    
public function index()
{
    $reviews = Review::with('user')->latest()->paginate(10); // Pagination
    return view('reviews.index', compact('reviews'));
}
    // ✅ Store Review
    public function store(Request $request)
    {
        $request->validate([
            'rating' => 'required|integer|min:1|max:5',
            'comment' => 'nullable|string|max:1000',
        ]);

        Review::create([
            'user_id' => Auth::id(),
            'rating' => $request->rating,
            'comment' => $request->comment,
        ]);

        return back()->with('success', 'Review submitted successfully.');
    }

    // ✅ Optional destroy method (only if needed for user-side)
    public function destroy(Review $review)
    {
        $review->delete();
        return back()->with('success', 'Review deleted successfully.');
    }
}