<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use App\Models\Slider;
use Illuminate\Http\Request;
use Illuminate\View\View;

class HomeController extends Controller
{
    public function index(Request $request): View
    {
        $sliders = Slider::where('is_active', true)->orderBy('sort_order')->get();
        $categories = Category::whereNull('parent_id')->where('is_active', true)->orderBy('sort_order')->limit(12)->get();
        $featured = Product::where('is_active', true)->where('is_featured', true)
            ->with(['category', 'images'])
            ->withCount('productRatings')
            ->withAvg('productRatings', 'rating')
            ->orderBy('sort_order')
            ->limit(12)
            ->get();
        $bestDeals = Product::where('is_active', true)
            ->whereNotNull('discount_price')
            ->where('discount_start', '<=', now())
            ->where('discount_end', '>=', now())
            ->with(['category', 'images'])
            ->withCount('productRatings')
            ->withAvg('productRatings', 'rating')
            ->orderBy('sort_order')
            ->limit(12)
            ->get();
        $popular = Product::where('is_active', true)
            ->with(['category', 'images'])
            ->withCount('productRatings')
            ->withAvg('productRatings', 'rating')
            ->orderBy('sort_order')
            ->limit(12)
            ->get();

        return view('store.home', compact('sliders', 'categories', 'featured', 'bestDeals', 'popular'));
    }
}
