<?php

require_once('../../core/backend.php');

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

require_once('Email.php');
require_once('EmailApi.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] == 'email') {
    $api = new EmailApi($requestUri, false);
	} else {
		echo json_encode(Array("message" => 'Invalid API request'));
	}
	echo $api->run();
}
catch (Exception $e) {
	echo json_encode(Array("message" => $e->getMessage()));
}

/**
 * @OA\Info(
 *   title="Email Endpoint API",
 *   version="1.0",
 *   @OA\Contact(
 *     email="support@float.sg"
 *   )
 * )
 */

/**
 * @OA\Schema(
 *   schema="receivers",
 *   type="object",
 *      @OA\Property(
 *          property="email",
 *          description="Email of receiver",
 *          type="string",
 *          format="string",
 *          example="jack@goldenowl.asia"
 *      ),
 *      @OA\Property(
 *          property="name",
 *          description="Name of receiver",
 *          type="string",
 *          format="string",
 *          example="Jack Nguyen",
 *      ),
 *      @OA\Property(
 *          property="substitutions",
 *          description="Receiver's personal information for email"
 *          type="object"
 *          example="{ "{{key}}": "value" }"
 *      )
 * )
 */

/**
 * @OA\Schema(
 *   schema="create_params",
 *   type="object",
 *               @OA\Property(
 *                   property="name",
 *                   description="Name of email template",
 *                   type="string",
 *                   format="string",
 *                   example="welcome"
 *               ),
 *               @OA\Property(
 *                   property="data",
 *                   description="Global dynamic data for the template",
 *                   type="object",
 *                   properties:
 *                      key:
 *                        type: string,
 *                        format: "{{key_name}}"
 *                      value:
 *                        type: string
 *                   example= {
 *                      "{{contactus}}": "https://float.sg"
 *                    }
 *               ),
 *               @OA\Property(
 *                   property="receivers",
 *                   description="List email's receivers",
 *                   type="array",
 *                   items="$ref: '#/components/schemas/receivers' "
 *                   
 *               )
 * )
*/