<?php

namespace App\Http\Controllers;

use App\Services\SearchService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function __construct(private readonly SearchService $service) {}

    public function buscaPrestadores(Request $request): JsonResponse
    {
        try {
            $dados = $this->service->buscaPrestadores($request->query('name'), $request->query('date'));

            return $this->successResponse(payload: $dados);
        } catch (\Exception $e) {
            return $this->errorResponse(message: __('messages.error_fetching_data'), code: 500, exception: $e);
        }
    }

    public function perfilPrestador(int $id): JsonResponse
    {
        try {
            $dados = $this->service->perfilPrestador($id);

            return $this->successResponse(payload: $dados);
        } catch (ModelNotFoundException $e) {
            return $this->errorResponse(message: __('messages.not_found'), code: 404, exception: $e);
        } catch (\Exception $e) {
            return $this->errorResponse(message: __('messages.error_fetching_data'), code: 500, exception: $e);
        }
    }
}
