<?php

declare(strict_types=1);

require dirname(__DIR__) . '/autoload.php';

use PhpWA\Client;
use PhpWA\Config\ClientOptions;

$workDirectory = getenv('PHPWA_WORK_DIR') ?: '';
$accountId = getenv('PHPWA_ACCOUNT_ID') ?: '';
$encodedKey = getenv('PHPWA_STORAGE_KEY_BASE64') ?: '';
$recipient = getenv('PHPWA_RECIPIENT_PHONE') ?: '';
$text = getenv('PHPWA_TEXT') ?: '';
$storageKey = base64_decode($encodedKey, true);
if ($workDirectory === '' || $accountId === '' || $recipient === '' || $text === ''
    || !is_string($storageKey) || strlen($storageKey) !== 32) {
    throw new RuntimeException('PhpWA send environment is incomplete.');
}

$client = new Client(new ClientOptions($accountId, $workDirectory, $storageKey));
$result = $client->withOpenSession(static function (Client $online) use ($recipient, $text) {
    return $online->sendTextToPhone($recipient, $text);
}, 20.0);
fwrite(STDOUT, 'Message staged with id ' . $result->messageId() . PHP_EOL);
