<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\ProductManagement\Product;

class ProductController extends Controller{

    public function show($slug){
        $product = Product::with('media', 'category', 'tags', 'approvedReviews')
                            ->withCount('media')
                            ->withAvg('approvedReviews', 'rating')
                            ->withCount('approvedReviews')
                            ->active()
                            ->whereSlug($slug)
                           // ->availables()
                            //->hasQuantity()
                            ->activeCategory()
                            ->firstOrFail();

       $relatedProducts = Product::with('media')
                                 ->whereHas('category', function ($query) use ($product) {
                                    return $query->whereId($product->category_id);
                                 })
                                ->where('id', '<>', $product->id)
                                ->inRandomOrder()
                                ->active()
                                ->take(5)
                                ->get(['id', 'slug', 'name', 'price']);

        return view('frontend.product.show', compact('product', 'relatedProducts'));
    }
}
