<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProviderServicesTypeRequest;
use App\Http\Resources\ProviderServicesTypeResource;
use App\Services\ProviderServicesTypeService;
use Illuminate\Http\JsonResponse;

class ProviderServicesTypeController extends Controller
{
    protected ProviderServicesTypeService $providerServicesTypeService;

    public function __construct(ProviderServicesTypeService $providerServicesTypeService)
    {
        $this->providerServicesTypeService = $providerServicesTypeService;
    }

    public function index(int $providerId): JsonResponse
    {
        $providerServicesTypes = $this->providerServicesTypeService->getByProvider($providerId);

        return $this->successResponse(
            payload: ProviderServicesTypeResource::collection($providerServicesTypes)
        );
    }

    public function store(ProviderServicesTypeRequest $request, int $providerId): JsonResponse
    {
        $data = $request->validated();

        $data['provider_id'] = $providerId;

        $providerServicesType = $this->providerServicesTypeService->create($data);

        return $this->successResponse(
            payload: new ProviderServicesTypeResource($providerServicesType->load('serviceType')),
            code: 201
        );
    }

    public function destroy(int $providerId, int $id): JsonResponse
    {
        $this->providerServicesTypeService->delete($id);

        return $this->successResponse();
    }
}
