<?php

namespace App\Http\Controllers\Admin\Api;

use App\Http\Controllers\Controller;
use App\Models\Appointment;
use App\libs\Response\GlobalApiResponse;
use App\libs\Response\GlobalApiResponseCodeBook;
use App\Services\Admin\AppointmentService;
use Carbon\Carbon;
use Illuminate\Http\Request;

class AppointmentsController extends Controller
{
    private $appointmentService;

    public function __construct(AppointmentService $appointmentService) {
        $this->appointmentService = $appointmentService;
    }

    public function appointmentsList(Request $request){
        $data = $this->appointmentService->appointmentsList($request);
        return (new GlobalApiResponse())->success('Appointments list',1,$data);
    }

    public function getAppointmentDetail($id){
        $data = $this->appointmentService->getAppointmentDetail($id);
        return (new GlobalApiResponse())->success('Appointments Details',1,$data);
    }

    public function createAppointment(Request $request){
        $this->appointmentService->createAppointment($request->all());
        if($this->appointmentService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->appointmentService->getErrors()[0]['message'], $this->appointmentService->getErrors());
        }
        return (new GlobalApiResponse())->success('Successfully created');
    }

    public function updateAppointment(Request $request, $id){
        $this->appointmentService->updateAppointment($request->all(), $id);
        if($this->appointmentService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->appointmentService->getErrors()[0]['message'], $this->appointmentService->getErrors());
        }
        return (new GlobalApiResponse())->success('Successfully updated');
    }

    public function deleteAppointment(Request $request){
        if (empty($request->appointment_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "IDs not found!");
        }
        auth()->user()->appointments()->whereIn('id', $request->appointment_ids)->delete();
        return (new GlobalApiResponse())->success("Successfully deleted");
    }
}
