<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Category;
use App\Models\Item;

class CategoryController extends Controller
{
    public function index()
    {
        $categories = Category::where('is_active', true)
            ->orderBy('sort_order')
            ->orderBy('name')
            ->get();

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

    public function show(Category $category)
    {
        $query = Item::with(['user', 'category', 'primaryPhoto'])
            ->whereIn('status', ['active', 'sale_pending'])
            ->where('category_id', $category->id)
            ->whereIn('listing_type', ['sale', 'inventory_open'])
            ->whereHas('user', function($q) {
                $q->where('is_active', true);
            });

        $items = $query->latest()->paginate(12);

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