customScheduleService = $customScheduleService; } public function index(): JsonResponse { $customSchedules = $this->customScheduleService->getAll(); return $this->successResponse( CustomScheduleResource::collection($customSchedules), ); } public function store(CustomScheduleRequest $request): JsonResponse { try { $validated = $request->validated(); $customSchedules = $this->customScheduleService->create($validated); $count = count($customSchedules); $message = $count > 1 ? "{$count} oportunidades criadas com sucesso!" : __("messages.created"); return $this->successResponse( payload: CustomScheduleResource::collection($customSchedules), message: $message, code: 201, ); } catch (\Exception $e) { return $this->errorResponse($e->getMessage(), 422); } } public function show(string $id): JsonResponse { $customSchedule = $this->customScheduleService->getById($id); return $this->successResponse( new CustomScheduleResource($customSchedule), ); } public function update(CustomScheduleRequest $request, string $id): JsonResponse { try { $customSchedule = $this->customScheduleService->update($id, $request->validated()); return $this->successResponse( payload: new CustomScheduleResource($customSchedule), message: __("messages.updated"), ); } catch (\Exception $e) { return $this->errorResponse($e->getMessage(), 422); } } public function destroy(string $id): JsonResponse { try { $this->customScheduleService->delete($id); return $this->successResponse( message: __("messages.deleted"), ); } catch (\Exception $e) { return $this->errorResponse($e->getMessage(), 422); } } public function groupedByClientCustom(): JsonResponse { $grouped = $this->customScheduleService->getSchedulesCustomGroupedByClient(); return $this->successResponse($grouped); } public function available(CustomScheduleRequest $request) { try { $providerId = $request->query('provider_id'); if (!$providerId) { return $this->errorResponse('Provider ID é obrigatório', 400); } $opportunities = $this->customScheduleService->getAvailableOpportunities($providerId); return $this->successResponse($opportunities); } catch (\Exception $e) { Log::error('Error fetching available opportunities: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 500); } } public function providerProposals(CustomScheduleRequest $request) { try { $providerId = $request->query('provider_id'); if (!$providerId) { return $this->errorResponse('Provider ID é obrigatório', 400); } $proposals = $this->customScheduleService->getProviderProposals($providerId); return $this->successResponse($proposals); } catch (\Exception $e) { Log::error('Error fetching provider proposals: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 500); } } public function opportunityProposals($scheduleId) { try { $proposals = $this->customScheduleService->getOpportunityProposals($scheduleId); return $this->successResponse($proposals); } catch (\Exception $e) { Log::error('Error fetching opportunity proposals: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 500); } } public function propose(CustomScheduleProposeRequest $request, $scheduleId) { try { $providerId = $request->input('provider_id'); if (!$providerId) { return $this->errorResponse('Provider ID é obrigatório', 400); } $proposal = $this->customScheduleService->proposeOpportunity($scheduleId, $providerId); return $this->successResponse($proposal, 'Proposta enviada com sucesso!', 201); } catch (\Exception $e) { Log::error('Error creating proposal: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 400); } } public function acceptProposal($proposalId) { try { $schedule = $this->customScheduleService->acceptProposal($proposalId); return $this->successResponse($schedule, 'Prestador aceito com sucesso!'); } catch (\Exception $e) { Log::error('Error accepting proposal: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 400); } } public function refuseProposal($proposalId) { try { $this->customScheduleService->refuseProposal($proposalId); return $this->successResponse(null, 'Prestador recusado.'); } catch (\Exception $e) { Log::error('Error refusing proposal: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 400); } } public function getProvidersProposalsAndOpportunities($providerId) { try { $data = $this->customScheduleService->getProvidersProposalsAndOpportunities($providerId); return $this->successResponse($data); } catch (\Exception $e) { Log::error('Error fetching provider proposals and opportunities: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 500); } } public function verifyCode($scheduleId, CustomScheduleVerifyCodeRequest $request) { try { $result = $this->customScheduleService->verifyScheduleCode($scheduleId, $request->input('code')); return $this->successResponse($result, 'Código verificado com sucesso!'); } catch (\Exception $e) { Log::error('Error verifying schedule code: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 500); } } public function refuseOpportunity($scheduleId, CustomScheduleRefuseOpportunityRequest $request) { try { $this->customScheduleService->refuseOpportunity($scheduleId, $request->input('provider_id')); return $this->successResponse(null, 'Oportunidade recusada.'); } catch (\Exception $e) { Log::error('Error refusing opportunity: ' . $e->getMessage()); return $this->errorResponse($e->getMessage(), 400); } } }