<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProviderBlockedDayRequest;
use App\Http\Resources\ProviderBlockedDayResource;
use App\Services\ProviderBlockedDayService;
use Illuminate\Http\JsonResponse;

class ProviderBlockedDayController extends Controller
{
    public function __construct(
        private readonly ProviderBlockedDayService $service
    ) {}

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

        return $this->successResponse(
            payload: ProviderBlockedDayResource::collection($blockedDays),
        );
    }

    public function show(int $id): JsonResponse
    {
        $blockedDay = $this->service->findById($id);

        return $this->successResponse(
            payload: new ProviderBlockedDayResource($blockedDay),
        );
    }

    public function store(ProviderBlockedDayRequest $request): JsonResponse
    {
        $blockedDay = $this->service->create($request->validated());

        return $this->successResponse(
            payload: new ProviderBlockedDayResource($blockedDay),
            message: __('messages.created'),
            code:    201,
        );
    }

    public function update(ProviderBlockedDayRequest $request, int $id): JsonResponse
    {
        $blockedDay = $this->service->findById($id);

        $blockedDay = $this->service->update($blockedDay, $request->validated());

        return $this->successResponse(
            payload: new ProviderBlockedDayResource($blockedDay),
            message: __('messages.updated'),
        );
    }

    public function destroy(int $id): JsonResponse
    {
        $blockedDay = $this->service->findById($id);

        $this->service->delete($blockedDay);

        return $this->successResponse(
            message: __('messages.deleted'),
            code:    204,
        );
    }
}
