<?php

namespace Tests;

use Dotenv\Dotenv;
use PHPUnit\Framework\TestCase;
use Okolaa\TermiiPHP\Termii;

class TermiiTest extends TestCase
{
  protected $verifySSL = false;

  public function __construct()
  {
    parent::__construct();
    $dotenv = Dotenv::createImmutable(__DIR__ . "/..");
    $dotenv->load();
  }

  /**
   * check if the Termii has no syntax error 
   */
  public function testCheckForSyntaxError()
  {
    $termii = new Termii;
    $this->assertTrue(is_object($termii));
    unset($termii);
  }

  public function testHasApiKeyAttribute()
  {
    $this->assertClassHasAttribute("apiKey", Termii::class);
  }

  public function testHasSenderIdAttribute()
  {
    $this->assertClassHasAttribute("senderId", Termii::class);
  }

  public function testCanSendMessage()
  {
    $termii = new Termii($_ENV["TERMII_SENDER_ID"], $_ENV["TERMII_API_KEY"]);

    // Disable Verify SSL in Guzzle
    $termii->verifySSL = $this->verifySSL;

    $sent = $termii->sendMessage([
      "phone_number" => $_ENV["TEST_PHONE_NUMBER"],
      "message" => "Unit Test Message"
    ]);

    $this->assertIsArray($sent, "Invalid Response");
    $this->assertArrayHasKey("message_id", $sent);
    unset($termii);
  }

  public function testCanSendToken()
  {
    $termii = new Termii($_ENV["TERMII_SENDER_ID"], $_ENV["TERMII_API_KEY"]);

    // Disable Verify SSL in Guzzle
    $termii->verifySSL = $this->verifySSL;

    //Set Options
    $termii->setMaxAttempts(2);
    $termii->setPinTimeToLive(5);
    $termii->SetPinLength(4);
    $termii->setPinType("NUMERIC");
    $termii->setMaxAttempts(1);

    $sent = $termii->sendToken([
      "phone_number" => $_ENV["TEST_PHONE_NUMBER"],
      "message" => "Your pin is < _pin_ >"
    ]);

    $this->assertArrayHasKey("pinId", $sent);
  }

  public function testVerifyToken()
  {
    $termii = new Termii($_ENV["TERMII_SENDER_ID"], $_ENV["TERMII_API_KEY"]);

    // Disable Verify SSL in Guzzle
    $termii->verifySSL = $this->verifySSL;

    $response = $termii->verifyToken([
      "pin_id" => "f862cb33-9dd3-42b3-b705-18200c0e800f",
      "pin" => "1234",
    ]);

    $this->assertIsArray($response, "Invalid Response");
  }

  public function testInAppToken()
  {
    $termii = new Termii($_ENV["TERMII_SENDER_ID"], $_ENV["TERMII_API_KEY"]);

    // Disable Verify SSL in Guzzle
    $termii->verifySSL = $this->verifySSL;

    $response = $termii->InAppToken([
      "phone_number" => $_ENV["TEST_PHONE_NUMBER"],
    ]);

    $this->assertIsArray($response, "Invalid Response");
    $this->assertEquals("success", $response["status"]);
    $this->assertIsArray($response["data"]);
  }

  public function testSendWithAutoGeneratedNumber()
  {
    $termii = new Termii();
    $termii->setAPIKey($_ENV["TERMII_API_KEY"]);

    // Disable Verify SSL in Guzzle
    $termii->verifySSL = $this->verifySSL;

    $response = $termii->sendWithAutoGeneratedNumber([
      "phone_number" => $_ENV["TEST_PHONE_NUMBER"],
      'message'      => "TermiiPHP Unit Test",
    ]);

    $this->assertIsArray($response, "Invalid Response");
    $this->assertEquals("ok", $response["code"]);
  }

  public function testGetSenderIds()
  {
    $termii = new Termii();
    $termii->setAPIKey($_ENV["TERMII_API_KEY"]);

    // Disable Verify SSL in Guzzle
    $termii->verifySSL = $this->verifySSL;

    $response = $termii->getSenderIds();

    $this->assertIsArray($response, "Invalid Response");
    $this->assertIsArray($response["data"]);
  }
}
