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

/* ----------------------------------------------------------------------------
 * Easy!Appointments - Open Source Web Scheduler
 *
 * @package     EasyAppointments
 * @author      A.Tselegidis <alextselegidis@gmail.com>
 * @copyright   Copyright (c) 2013 - 2020, Alex Tselegidis
 * @license     https://opensource.org/licenses/GPL-3.0 - GPLv3
 * @link        https://easyappointments.org
 * @since       v1.2.0
 * ---------------------------------------------------------------------------- */

/**
 * Admins API v1 controller.
 *
 * @package Controllers
 */
class Admins extends EA_Controller {
    /**
     * Class Constructor
     */
    public function __construct()
    {
        parent::__construct();

        $this->load->model('admins_model');

        $this->load->library('api');

        $this->api->cors();

        $this->api->auth();

        $this->api->model('admins_model');
    }

    /**
     * Get a single admin or an admin collection.
     *
     * @param int|null $id Admin ID.
     */
    public function get(int $id = NULL)
    {
        try
        {
            $where = $id ? ['id' => $id] : NULL;

            $keyword = $this->api->request_keyword();

            $limit = $this->api->request_limit();

            $offset = $this->api->request_offset();

            $order_by = $this->api->request_order_by();

            $fields = $this->api->request_fields();

            $admins = empty($keyword)
                ? $this->admins_model->get($where, $limit, $offset, $order_by)
                : $this->admins_model->search($keyword, $limit, $offset, $order_by);

            foreach ($admins as &$admin)
            {
                $this->admins_model->api_encode($admin);

                if ( ! empty($fields))
                {
                    $this->admins_model->only($admin, $fields);
                }
            }

            $response = $id && ! empty($admins) ? $admins[0] : $admins;

            if ( ! $response)
            {
                response('Not Found', 404);

                return;
            }

            json_response($response);
        }
        catch (Throwable $e)
        {
            json_exception($e);
        }
    }

    /**
     * Create an admin.
     */
    public function post()
    {
        try
        {
            $admin = request();

            $this->admins_model->api_decode($admin);

            if (array_key_exists('id', $admin))
            {
                unset($admin['id']);
            }

            if ( ! array_key_exists('settings', $admin))
            {
                throw new Exception('No settings property provided.');
            }

            $admin_id = $this->admins_model->save($admin);

            $created_admin = $this->admins_model->find($admin_id);

            $this->admins_model->api_encode($created_admin);

            json_response($created_admin, 201);
        }
        catch (Throwable $e)
        {
            json_exception($e);
        }
    }

    /**
     * Update an admin.
     *
     * @param int $id Admin ID.
     */
    public function put(int $id)
    {
        try
        {
            $occurrences = $this->admins_model->get(['id' => $id]);

            if (empty($occurrences))
            {
                response('', 404);

                return;
            }

            $original_admin = $occurrences[0];

            $admin = request();

            $this->admins_model->api_decode($admin, $original_admin);

            $admin_id = $this->admins_model->save($admin);

            $updated_admin = $this->admins_model->find($admin_id);

            $this->admins_model->api_encode($updated_admin);

            json_response($updated_admin);
        }
        catch (Throwable $e)
        {
            json_exception($e);
        }
    }

    /**
     * Delete an admin.
     *
     * @param int $id Admin ID.
     */
    public function delete(int $id)
    {
        try
        {
            $occurrences = $this->admins_model->get(['id' => $id]);

            if (empty($occurrences))
            {
                response('', 404);

                return;
            }

            $this->admins_model->delete($id);
        }
        catch (Throwable $e)
        {
            json_exception($e);
        }
    }
}