<?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;

/**
 * Providers Controller
 *
 * @package Controllers
 */
class Sync extends API_V1_Controller {
    /**
     * Providers Resource Parser
     *
     * @var \EA\Engine\Api\V1\Parsers\Providers
     */
    protected $parser;

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

    /**
     * GET method to sync appointments, services, providers, customers
     */
    public function get()
    {
        try
        {
            /// To get all appointments ///
			$where = [
                'is_unavailable' => FALSE
            ];

            if ($id !== NULL)
            {
                $where['id'] = $id;
            }

            $appointments = $this->appointments_model->get_batch($where, NULL, NULL, NULL, array_key_exists('aggregates', $_GET));
            
			$conditions = $id !== NULL ? ['id' => $id] : NULL;
			/// To get all services ///
            $services = $this->services_model->get_batch($conditions);
			
			/// To get all providers ///
            $providers = $this->providers_model->get_batch($conditions);
			
			/// To get all customers ///
			$customers = $this->customers_model->get_batch($conditions);
			
			$responseArray['appointments'] = $appointments;
			$responseArray['services']     = $services;
			$responseArray['providers']    = $providers;
			$responseArray['customers']    = $customers;			
			
			$this->output
				 ->set_status_header('200')
				 ->set_content_type('application/json')
				 ->set_output(json_encode($responseArray));

        }
        catch (Exception $exception)
        {
            $this->handle_exception($exception);
        }
    }

}
