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

require_once __DIR__ . '/API_V1_Controller.php';
require_once __DIR__ . '/../../Appointments.php';

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


/**
 * First_available Controller
 *
 * @package Controllers
 */
class First_available extends API_V1_Controller {
    /**
     * Class Constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('appointments_model');
        $this->load->model('providers_model');
        $this->load->model('services_model');
        $this->load->model('settings_model');
        $this->load->library('availability');
    }

    /**
     * GET API Method
     *
     * Provide the "providerId" and "date" GET parameters to get the availabilities for a specific date.
     * If no "date" was provided then the current date will be used.
     */
    public function get()
    {
        try
        {	
			$httpStatusCode = 200;
			$responseArray['return_message'] = "";
			
            $provider_id = $this->input->get('doctor_id');
			
			if( empty( $provider_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( $provider_id ) ) {
				$httpStatusCode = 400;
				$responseArray['return_message'] = 'Invalid doctor_id';
				$this->output
					 ->set_status_header($httpStatusCode)
					 ->set_content_type('application/json')
					 ->set_output(json_encode($responseArray));
				return;	 
			}			
	
			$providerServices = $this->db->get_where('services_providers', ['id_users' => $provider_id])->row_array();
			
            $service_id = !empty( $providerServices ) ? $providerServices['id_services'] : "";
			
			if( empty( $service_id ) ) {
				$httpStatusCode = 400;
				$responseArray['return_message'] = 'Doctor was not mapped to any service';
				$this->output
					 ->set_status_header($httpStatusCode)
					 ->set_content_type('application/json')
					 ->set_output(json_encode($responseArray));
				return;	 
			}			

            $date = $this->input->get('date');

            if ( ! $date)
            {
                $date = date('Y-m-d');
            }
			
            $provider = $this->providers_model->get_row($provider_id);

            $service = $this->services_model->get_row($service_id);

            $available_hours = $this->availability->get_available_hours($date, $service, $provider);
			
			if( empty( $available_hours ) ) {
				$httpStatusCode = 500;
				$responseArray['return_message'] = 'Failed to get first available timeslot';
				$this->output
					 ->set_status_header($httpStatusCode)
					 ->set_content_type('application/json')
					 ->set_output(json_encode($responseArray));
				return;	 
			} else {
				$httpStatusCode = 200;
				$responseArray['return_message'] = 'succes';
				$responseArray['has_timeslots'] = 'true';
				$responseArray['first_timeslot'] = date ('Y-m-d H:i:s',strtotime($date.' '.$available_hours[0]));
				$this->output
					 ->set_status_header($httpStatusCode)
					 ->set_content_type('application/json')
					 ->set_output(json_encode($responseArray));
				return;	 
			}				
        }
        catch (Exception $exception)
        {
            $this->handle_exception($exception);
        }
    }
}
