<?php

declare(strict_types=1);

$base = 'http://karviapi1.de';
$token = 'a1b2c3d4e5f6789012345678abcdef01';
$id = '123';

$paths = [];
foreach ([
    "POST /karvipartner/contracts/sign/{$token}",
    "POST /karvipartner/contracts/{$token}/sign",
    "POST /karvipartner/contracts/{$id}/sign",
    "GET /karvipartner/contracts/{$id}",
    "GET /karvipartner/contracts/{$token}",
    "PUT /karvipartner/contracts/{$token}",
    "PATCH /karvipartner/contracts/{$token}",
    "POST /karvipartner/contracts/{$token}/duplicate",
    "POST /karvipartner/contracts/duplicate/{$token}",
    "DELETE /karvipartner/contracts/{$id}",
    "POST /karvipartner/contracts",
    "GET /api/v1/karvipartner/contracts",
    "POST /api/v1/karvipartner/contracts/sign/{$token}",
] as $line) {
    [$method, $path] = explode(' ', $line, 2);
    $paths[] = [$method, $path];
}

foreach ($paths as [$method, $path]) {
    $url = $base . $path;
    $ch = curl_init($url);
    $body = json_encode(['terms_accepted' => true, 'raw_data' => []]);
    if ($method === 'POST' && !str_contains($path, 'sign') && !str_contains($path, 'duplicate')) {
        $body = json_encode(['restaurant_name' => 'Test', 'email' => 'test@test.com']);
    }
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 12,
        CURLOPT_CUSTOMREQUEST => $method,
        CURLOPT_HTTPHEADER => ['Accept: application/json', 'Content-Type: application/json'],
        CURLOPT_POSTFIELDS => in_array($method, ['POST', 'PUT', 'PATCH'], true) ? $body : null,
    ]);
    $raw = curl_exec($ch);
    $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    $msg = '';
    if (is_string($raw)) {
        $d = json_decode($raw, true);
        $msg = is_array($d) ? (string)($d['message'] ?? '') : substr($raw, 0, 60);
    }
    echo sprintf("%-6s %-55s HTTP %3d  %s\n", $method, $path, $status, $msg);
}
