<?php

namespace App\Http\Controllers\Web;

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

class CategoryController extends Controller
{
    public function index(Request $request): View
    {
        $categories = Category::whereNull('parent_id')
            ->where('is_active', true)
            ->with('activeChildren')
            ->orderBy('sort_order')
            ->get();

        return view('store.categories.index', compact('categories'));
    }

    public function show(Request $request, Category $category): View
    {
        $category->load('activeChildren');
        $products = $category->products()
            ->where('is_active', true)
            ->with(['category', 'images', 'activeVariants'])
            ->withCount('productRatings')
            ->withAvg('productRatings', 'rating')
            ->orderBy('sort_order')
            ->paginate(24)
            ->withQueryString();

        return view('store.categories.show', compact('category', 'products'));
    }
}
