<?php
require_once('../../core/backend.php');
require_once('../constants.php');

require_once('../common/Api.php');
require_once('../common/Db.php');

require_once('Callback.php');
require_once('AccountAPI.php');

$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');

header("Access-Control-Allow-Origin: *");
header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, client_id");
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
header('Content-type: application/json');

if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) {
    exit();
}

$headers = getallheaders();
if ((!isset($headers["Authorization"]) || substr($headers["Authorization"],-strlen($httpAuthToken))!=$httpAuthToken) &&
    (!isset($headers["authorization"]) || substr($headers["authorization"],-strlen($httpAuthToken))!=$httpAuthToken)) {
    header('HTTP/1.1 401 Unauthorized');
    header('Status: 401 Unauthorized');
    echo "{\"status\":\"Missing authorization\"}";
    exit();
}

try {
	if (strpos($_SERVER['REQUEST_URI'],'/api/')===false) {
		throw new Exception("Invalid API request");
	}
	$requestUri = explode('/', trim($_SERVER['REQUEST_URI'],'/'));
	while (array_shift($requestUri) !== 'api') {
	};
	if ($requestUri[0]=='account') {
		$api = new AccountApi($requestUri);
	}
	else {
		echo json_encode(Array('error' => 'Invalid API request'));
	}
	echo $api->run();
}
catch (Exception $e) {
	echo json_encode(Array('error' => $e->getMessage()));
}

/**
 * @OA\Info(
 *   title="Call Backend Endpoint API",
 *   version="0.1",
 *   @OA\Contact(
 *     email="support@float.sg"
 *   )
 * )
 */
/**
 * @OA\Schema(
 *   schema="member_id",
 *   type="integer",
 *   format="int64",
 *   description="The unique identifier of a member in our system"
 * )
 */
    /**
     * @OA\Schema(
     *   schema="account_request",
     *   type="object",
     *               @OA\Property(
     *                   property="member_id",
     *                   description="The unique identifier of a member in our system",
     *                   type="int",
	 *                   format="int64",
	 *                   example=22
     *               ),
     *               @OA\Property(
     *                   property="last_acct",
     *                   description="Last date of the account connection",
     *                   type="string",
	 *                   format="date-time",
	 *                   example="2019-05-06"
     *               ),
     *               @OA\Property(
     *                   property="count_acct",
     *                   description="Count of the account items",
     *                   type="int",
	 *                   format="int64",
	 *                   example=1
     *               ),
	 * )
     */
    /**
     * @OA\Schema(
     *   schema="member_data",
     *   type="object",
     *               @OA\Property(
     *                   property="id",
     *                   description="The unique identifier of a member in our system",
     *                   type="int",
	 *                   format="int64",
	 *                   example=22
     *               ),
     *               @OA\Property(
     *                   property="last_acct",
     *                   description="Last date of the account connection",
     *                   type="string",
	 *                   format="date-time",
	 *                   example="2019-05-06"
     *               ),
     *               @OA\Property(
     *                   property="count_acct",
     *                   description="Count of the account items",
     *                   type="int",
	 *                   format="int64",
	 *                   example=1
     *               ),
	 * )
     */
