<?php

class AddressToAreaApi extends Api {
	public $apiName = 'addresstoarea';

	public function __construct($requestUri, $encryption=true) {
		$encryption = false; // We do not need encryption for this class
		parent::__construct($requestUri, $encryption);
		//$this->encryption = $encryption;
	}

	/**
	 * @OA\Get(
	 *   path="/SAVVY/geofencearea/api/addresstoarea?address_id={address_id}",
	 *   security={{"token": {}}},
	 *   summary="Geocode address id to a geofence area",
	 *   @OA\Response(
	 *     response="200",
	 *     description="area list address belongs to",
	 *     @OA\JsonContent(
	 *       type="object"
	 *     )
	 *   )
	 * )
	 */
	public function indexAction() {
		$message = 'No areas found';

		$address_id = $this->requestParams['address_id'] ?? 0;
        $city = $this->requestParams['city'] ?? 0;
        $country = $this->requestParams['country'] ?? '';

		try {
			if ($address_id<1) {
				throw new Exception("Invalid address ID");
			}

      if (!in_array(strtoupper($country), ['SG','US']) ) { // Singapore and USA only for now
        throw new Exception("Invalid country");
      }

			$db = new Db();
			list($res, $err) = AddressToArea::get( $db->getConnect(), $address_id, $city, $country );
			return $this->response(
				[
					"areas" => $res,
					"error" => $err
				], 200 );

		} catch (Exception $e) {
			$message = $e->getMessage();
		}		
		return $this->response(
            [
				"areas" => NULL, 
                "error" => $message
			], 404);
	}

	public function viewAction() {

	}

	public function createAction() {

	}

	public function updateAction() {

	}

	public function deleteAction() {

	}

}
