<?php

namespace App\Http\Controllers;

use App\Http\Requests\ClientFavoriteProviderRequest;
use App\Http\Resources\ClientFavoriteProviderResource;
use App\Services\ClientFavoriteProviderService;
use Illuminate\Http\JsonResponse;

class ClientFavoriteProviderController extends Controller
{
    public function __construct(
        private ClientFavoriteProviderService $service
    ) {}

    public function index(int $clientId): JsonResponse
    {
        $favorites = $this->service->getByClientId($clientId);

        return $this->successResponse(
            payload: ClientFavoriteProviderResource::collection($favorites),
        );
    }

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

        return $this->successResponse(
            payload: new ClientFavoriteProviderResource($favorite),
        );
    }

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

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

    public function update(ClientFavoriteProviderRequest $request, int $id): JsonResponse
    {
        $favorite = $this->service->update($id, $request->validated());

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

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

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

    //

    public function getFavoritedProviders(int $clientId): JsonResponse
    {
        $providerIds = $this->service->getFavoritedProviderIds($clientId);

        return $this->successResponse(
            payload: $providerIds,
        );
    }
}
