<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Models\Review;
use App\Models\Seller;

class ReviewController extends Controller
{
    public function index()
    {
        $seller = Seller::where('user_id', auth()->id())->first();
        $reviews = Review::whereHas('product', fn($q) => $q->where('seller_id', $seller->id))
            ->with(['product', 'user'])
            ->orderBy('created_at', 'desc')
            ->paginate(15);
        
        $avgRating = Review::whereHas('product', fn($q) => $q->where('seller_id', $seller->id))->avg('rating');
        $totalReviews = Review::whereHas('product', fn($q) => $q->where('seller_id', $seller->id))->count();
        
        return view('seller.reviews.index', compact('reviews', 'avgRating', 'totalReviews'));
    }
}
