<?php

declare(strict_types=1);

$base = 'http://karviapi1.de';
$paths = [
    ['GET', '/karvipartner/contracts'],
    ['GET', '/karvipartner/contract'],
    ['GET', '/karvipartner/contracts/list'],
    ['POST', '/karvipartner/contracts/sign'],
    ['POST', '/karvipartner/sign'],
    ['GET', '/karvipartner/contracts/sign/abc123'],
    ['PUT', '/karvipartner/contracts/abc123'],
    ['PATCH', '/karvipartner/contracts/abc123'],
    ['GET', '/karvipartner/contracts/by-token/abc123'],
    ['GET', '/api/karvipartner/contracts'],
    ['POST', '/karvipartner/login'],
];

foreach ($paths as [$method, $path]) {
    $url = $base . $path;
    $ch = curl_init($url);
    $body = null;
    if ($method === 'POST' && str_contains($path, 'login')) {
        $body = json_encode(['email' => 'admin', 'password' => 'x']);
    } elseif ($method === 'POST' && str_contains($path, 'sign')) {
        $body = json_encode(['terms_accepted' => true, 'raw_data' => ['sign_token' => 'abc123']]);
    } elseif (in_array($method, ['PUT', 'PATCH'], true)) {
        $body = json_encode(['pdf_viewed_at' => date('c')]);
    }

    $opts = [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 12,
        CURLOPT_HTTPHEADER => ['Accept: application/json', 'Content-Type: application/json'],
        CURLOPT_CUSTOMREQUEST => $method,
    ];
    if ($body !== null) {
        $opts[CURLOPT_POSTFIELDS] = $body;
    }
    curl_setopt_array($ch, $opts);
    $raw = curl_exec($ch);
    $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $msg = '';
    if (is_string($raw)) {
        $decoded = json_decode($raw, true);
        $msg = is_array($decoded) ? (string)($decoded['message'] ?? substr($raw, 0, 80)) : substr($raw, 0, 80);
    }
    echo sprintf("%-6s %-42s HTTP %3d  %s\n", $method, $path, $status, $msg);
}
