<?php defined('BASEPATH') or exit('No direct script access allowed');

require_once __DIR__ . '/API_V1_Controller.php';

use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;

/**
 * Appointments Controller
 *
 * @package Controllers
 */
class Cancel_appointment extends API_V1_Controller {

    /**
     * Class Constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('appointments_model');
        $this->load->model('services_model');
        $this->load->model('providers_model');
        $this->load->model('customers_model');
        $this->load->model('settings_model');
        $this->load->library('synchronization');
        $this->load->library('notifications');
        $this->parser = new \EA\Engine\Api\V1\Parsers\Appointments;
    }


    /**
     * DELETE API Method
     *
     * @param int $id The record ID to be deleted.
     */
    public function post()
    {	
        try
        {	
			// Get request body //
            $request   = new Request();
            $reqParam = $request->get_body();	

            $id = $reqParam['appointment_id'];			
			if( empty( $id ) ) {
				$httpStatusCode = 400;
				$responseArray['return_message'] = 'Mandatory fields are not supplied';
				$this->output
					 ->set_status_header($httpStatusCode)
					 ->set_content_type('application/json')
					 ->set_output(json_encode($responseArray));
				return;	 
			} else if( ! is_numeric( $id )) {
				$httpStatusCode = 400;
				$responseArray['return_message'] = 'Invalid appointment_id';
				$this->output
					 ->set_status_header($httpStatusCode)
					 ->set_content_type('application/json')
					 ->set_output(json_encode($responseArray));
				return;	 
			}

            $appointment = $this->appointments_model->get_row($id);
			
			if( empty( $appointment ) ) {
				$httpStatusCode = 500;
				$responseArray['return_message'] = 'Failed to cancel appointment';
				$this->output
					 ->set_status_header($httpStatusCode)
					 ->set_content_type('application/json')
					 ->set_output(json_encode($responseArray));
				return;	 
			}	
			
            $service = $this->services_model->get_row($appointment['id_services']);
            $provider = $this->providers_model->get_row($appointment['id_users_provider']);
            $customer = $this->customers_model->get_row($appointment['id_users_customer']);
            $settings = [
                'company_name' => $this->settings_model->get_setting('company_name'),
                'company_email' => $this->settings_model->get_setting('company_email'),
                'company_link' => $this->settings_model->get_setting('company_link'),
                'date_format' => $this->settings_model->get_setting('date_format'),
                'time_format' => $this->settings_model->get_setting('time_format')
            ];

            $this->appointments_model->delete($id);

            $this->synchronization->sync_appointment_deleted($appointment, $provider);
            $this->notifications->notify_appointment_deleted($appointment, $service, $provider, $customer, $settings);

			$httpStatusCode = 200;
			$responseArray['return_message'] = 'Success';
			$this->output
				 ->set_status_header($httpStatusCode)
				 ->set_content_type('application/json')
				 ->set_output(json_encode($responseArray));
        }
        catch (Exception $exception)
        {
            $this->handle_exception($exception);
        }
    }
	
	
	
}
