<?php

namespace App\Http\Controllers\Api\PublicApi;

use App\Http\Controllers\Controller;
use App\Http\Resources\Category as ResourcesCategory;
use App\Models\Category;
use App\Models\ListingCategory;
use App\Services\ListingFeatureService;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function index (Request $request)
    {
        $query = Category::query()
        ->active()
        ->orderBy('sort_order');

        if($request->boolean('featured')){
            $query->featured();
        }




        return ResourcesCategory::collection($query->get());
    }

    /**
     * Bir kategoriye atanmış feature şemasını döndürür.
     * İlan oluşturma formu, dinamik alanları bu listeye göre çizer.
     */
    public function features(string $slug, ListingFeatureService $service)
    {
        $category = ListingCategory::where('slug', $slug)
            ->where('is_active', true)
            ->firstOrFail();

        return response()->json([
            'data' => $service->categoryFeatures($category),
        ]);
    }
}
