<?php

declare(strict_types=1);

require __DIR__ . '/../includes/bootstrap.php';
require __DIR__ . '/../includes/mvc.php';
require __DIR__ . '/../includes/api_service.php';

$api = \App\Services\ApiService::instance();

$signToken = trim((string)($argv[1] ?? 'bnrtfvwrtnqdbxxk4b6n8sxe0actexs4'));

echo "=== Sign contract (public) test ===\n\n";

$invalid = contract_service()->signContractPublic('badtoken000000000000000000000000', [
    'terms_accepted' => true,
    'raw_data' => ['signed_from' => 'test script'],
]);
echo 'Invalid: ' . ($invalid->success ? 'UNEXPECTED OK' : $invalid->message) . "\n\n";

$payload = contract_service()->buildPublicSignPayloadFromPost([
    'terms_accepted' => '1',
    'signed_from' => 'test script',
    'browser' => 'CLI',
    'device_type' => 'server',
]);
$payload['terms_accepted'] = true;

$result = contract_service()->signContractPublic($signToken, $payload);
echo 'Sign (' . $signToken . '): ' . ($result->success ? 'OK' : 'FAIL') . ' — ' . $result->message . "\n";
