<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Subcategory;
use App\Models\Product;

class CategoriesController extends Controller
{
    public function index()
{
    $categories = Category::with(['subcategories' => function($query) {
        $query->where('status', 1);
    }])->where('status', 1)->get();
    
    return view('categories.index', compact('categories'));
}

public function show($slug)
{
    $categories = Category::with(['subcategories' => function($query) {
        $query->where('status', 1);
    }])->where('slug', $slug)
        ->where('status', 1)
        ->firstOrFail();

    $subcategoryIds = $categories->subcategories->pluck('id');
    
    $products = Product::whereIn('subcategory_id', $subcategoryIds)
        ->where('is_active', 1)
        ->paginate(12);

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

}