<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProviderSpecialityRequest;
use App\Http\Resources\ProviderSpecialityResource;
use App\Services\ProviderSpecialityService;
use Illuminate\Http\JsonResponse;

class ProviderSpecialityController extends Controller
{
    protected ProviderSpecialityService $providerSpecialityService;

    public function __construct(ProviderSpecialityService $providerSpecialityService)
    {
        $this->providerSpecialityService = $providerSpecialityService;
    }

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

        return $this->successResponse(
            payload: ProviderSpecialityResource::collection($providerSpecialities)
        );
    }

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

        $data['provider_id'] = $providerId;

        $providerSpeciality = $this->providerSpecialityService->create($data);

        return $this->successResponse(
            payload: new ProviderSpecialityResource($providerSpeciality->load('speciality')),
            code: 201
        );
    }

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

        return $this->successResponse();
    }
}
