<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\CategoryResource;
use App\Models\ProductCategory;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;

class CategoryController extends Controller
{
    public function index(): JsonResponse
    {
        $categories = Cache::remember('categories.tree', 600, function () {
            return ProductCategory::with('children')
                ->whereNull('parent_id')
                ->orderBy('sort_order')
                ->get();
        });

        return response()->json(['data' => CategoryResource::collection($categories)]);
    }
}
