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

/**
 * Providers Controller
 *
 * @package Controllers
 */
class Appsettings extends API_V1_Controller {
    /**
     * Class Constructor
     */
    public function __construct()
    {
        parent::__construct();
		$this->load->model('settings_model');
    }

    /**
     * GET API Method
     *
     *  returns all settings values.
     */
    public function get($name = NULL)
    {
        try
        {			
			$valueArray = array('company_name',
								'company_email',
								'reschedule_expiry', 
								'cancel_expiry', 
								'booking_fee_price',
								'patient_file_expiry',
								'company_link',      
								'date_format',    
								'time_format',   
								'first_weekday',
								'display_duration',  
								'display_price',
								'display_location'
								); 
								
			$settings = $this->settings_model->get_settings();
			
			foreach($settings as $setting) {
				if(in_array($setting['name'],$valueArray)) {
					$responseArray['settings'][$setting['name']] = $setting['value'];
				}		
			}	
			
			$this->output
				 ->set_status_header('200')
				 ->set_content_type('application/json')
				 ->set_output(json_encode($responseArray['settings']));

        }
        catch (Exception $exception)
        {
            $this->handle_exception($exception);
        }
    }
	
    /**
     * POST API Method to update settings
     *
     */
    public function post()
    {
        try
        {			
			// Get request body //
            $request   = new Request();
            $reqParam = $request->get_body();			
			
			foreach($reqParam as $key => $value) {
				$status = "Update Failed.";
				$settingsId = $this->settings_model->set_setting($key, $value);
				
				if(!empty($settingsId)) {
					$status = "Updated Successfully.";
					$responseArray[$key] = $status;				
				}	
				
			}	
			
			$this->output
				 ->set_status_header('200')
				 ->set_content_type('application/json')
				 ->set_output(json_encode($responseArray));
	
		}
        catch (Exception $exception)
        {
            $this->handle_exception($exception);
        }
	}	
	

}
