<?php

namespace App\Http\Controllers;

use App\Http\Resources\DashboardClienteResource;
use App\Http\Resources\DashboardPrestadorResource;
use App\Services\DashboardService;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;

class DashboardController extends Controller
{
    public function __construct(private readonly DashboardService $service) {}

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

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

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

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

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

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