<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\RestaurantMenuCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Api\Helpers\RestaurantBranchFilterTrait;

class RestaurantMenuCategoryController extends Controller
{
    use RestaurantBranchFilterTrait;
    public function index(Request $request)
    {
        $query = RestaurantMenuCategory::where('status', true);

        // Apply restaurant and branch filtering
        $user = $request->user();
        $this->applyRestaurantBranchFilter($query, $user, $request);
        
        // Backward compatibility: Also filter by warehouse_id if provided
        $warehouseId = $request->input('warehouse_id');
        if ($warehouseId && !$request->has('branch_id')) {
            $query->where('warehouse_id', $warehouseId);
        }

        if ($request->has('search')) {
            $query->where('name', 'like', '%' . $request->search . '%');
        }

        $categories = $query->orderBy('sort_order')->orderBy('name')->distinct()->get();

        return response()->json($categories);
    }

    public function show(RestaurantMenuCategory $category)
    {
        return response()->json($category);
    }
}
