<?php

namespace App\Services\ThirdPartyApiServices\Amplivo;

use App\Services\ThirdPartyApiServices\BaseApiCall;

class AmplivoService extends BaseApiCall
{
    protected array $data = [];
    protected array $config = [];
    protected string $mode;

    public function __construct()
    {
        $this->config = config('amplivo.api');
        $this->mode = $this->config['mode'];
        if ($this->mode == 'live') {
            $this->setApiUrl($this->config['live_endpoint']);
        } elseif ($this->mode == 'test') {
            $this->setApiUrl($this->config['test_endpoint']);
        }
    }

    /**
     * Validate sponsor against valid email address
     *
     * @param $email
     * @param mixed $query
     * @param mixed $param
     * @return array|mixed|void
     */
    public function isValidEmail($email, $query = 'email', $param = 'userstatus/get')
    {
        $this->setQueryData($query, $email);
        return $this->callToApi($param);
    }

    /**
     * Validate sponsor against valid username
     *
     * @param $username
     * @param string $query
     * @param string $param
     * @return array|mixed|void
     */
    public function isValidUsername($username, string $query = 'username', string $param = 'userstatus/get')
    {
        $this->setQueryData($query, $username);
        return $this->callToApi($param);
    }

    /**
     * Validate sponsor against both valid email and username
     *
     * @param $email
     * @param $username
     * @param array|string[] $query
     * @param mixed $param
     * @return array|mixed|void
     */
    public function isValidEmailAndUsername($email, $username, array $query = ['email', 'Username'], $param = 'userstatus/get')
    {
        $this->setQueryData($query, array($email, $username));
        return $this->callToApi($param);
    }

    /**
     * @param $response
     * @return bool
     */
    public function checkResponseStatus($response): bool
    {
        return $response != "Not found" && ($response['Status'] == 'active' || $response['Status'] == 'abandoned');
    }
}
