<?php

class AccountApi extends Api
{
    public $apiName = 'account';

    public function indexAction()
    {
        return $this->response(
            array(
                'error' => 'Data not found'
            ), 404);
    }

    /**
     * Method GET
     * Get member record (by id)
     * http://DOMAIN/trips/1
     * @return string
     */
/**
 * @OA\Get(
 *   path="/SAVVY/callback/api/account/:id",
 *   summary="Get member account data by its unique ID",
 *   @OA\Parameter(
 *     name="id",
 *     in="path",
 *     required=true,
 *     @OA\Schema(ref="#/components/schemas/member_id")
 *   ),
 *   @OA\Response(
 *     response="200",
 *     description="Members data",
 *     @OA\JsonContent(
 *       type="object",
 *       @OA\Schema(ref="#/components/schemas/member_data")
 *     )
 *   )
 * )
 */
    public function viewAction()
    {
        //id must be the first parameter after /account/x
        $id = array_shift($this->requestUri);

        if($id && (int)$id>0){
            $db = new Db();
            $member = Callback::getMemberById($db->getConnect(), (int)$id);
            if(is_array($member) && count($member)>0){
                return $this->response($member, 200);
            }
        }
        return $this->response(
            array(
                'error'=> 'Data not found'
            ), 404);
    }
// *   description = "curl -d '{\"member_id\":22,\"last_acct\":\"2019-05-06\",\"count_acct\":1}' -X POST https://svrsavvy.sworks.float.sg/SAVVY/callback/api/account",

/**
 * @OA\Post(
 *   path="/SAVVY/callback/api/account",
 *   summary="Save account data within members record",
 *   @OA\Parameter(
 *     name="",
 *     description = "Account callback data",
 *     in="body",
 *     required=true,
 *     @OA\Schema(ref="#/components/schemas/account_request")
 *   ),
  *   @OA\Response(
 *     response="200",
 *     description="Members data",
 *     @OA\JsonContent(
 *       type="object",
 *       @OA\Schema(ref="#/components/schemas/member_data")
 *     )
 *   )
 * )
 */
    public function createAction()
    {
        $message = "Failed to save data";
        $member_id = $this->requestParams["member_id"] ?? 0;
        $last_acct = $this->requestParams["last_acct"] ?? "";
        $count_acct = $this->requestParams["count_acct"] ?? 0;

        if ($member_id>0 && $last_acct!="" && strtotime($last_acct)>0 && $count_acct>0) {
            $db = new Db();
            $member = Callback::getMemberById($db->getConnect(), (int)$member_id);
            if (isset($member["id"]) && $member["id"]>0) {
                $result = Callback::updateMember(
                    $db->getConnect(), 
                    (int)$member_id, 
                    $last_acct, 
                    $count_acct);
                if (is_array($result) && count($result)>0) {
                    return $this->response($result, 200);
                } else {
                    $message = "Failed to update member";
                }
            } else {
                $message = "Invalid member id";
            }
        } else {
            $message = "Invalid input $member_id>0 && $last_acct!= $count_acct";
        }
        return $this->response(
            array(
                "error" => $message
            ), 500);
    }

    public function updateAction()
    {
        return $this->response(
            array(
                "error" => "Update error"
            ), 400);
    }

    public function deleteAction()
    {
        return $this->response(
            array(
                "error" => "Delete error"
            ), 500);
    }
}
