<?php

namespace App\Http\Controllers\Api\Services;

use App\Http\Controllers\Api\BaseController;
use App\Models\BillService;
use App\Models\BillVariation;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ServiceController extends BaseController
{
    public function index(Request $request): JsonResponse
    {
        $query = BillService::active()->orderBy('sort_order');

        if ($category = $request->get('category')) {
            $query->where('category', $category);
        }

        $services = $query->get();

        return $this->successResponse($services);
    }

    public function show(BillService $service): JsonResponse
    {
        $service->load(['variations' => fn ($q) => $q->active()->orderBy('sort_order')]);

        return $this->successResponse($service);
    }

    public function variations(BillService $service): JsonResponse
    {
        $variations = BillVariation::where('bill_service_id', $service->id)
            ->active()
            ->orderBy('sort_order')
            ->get();

        return $this->successResponse([
            'service' => $service,
            'variations' => $variations,
        ]);
    }
}
