<?php
declare(strict_types=1);
require __DIR__.'/lib/bootstrap.php';
try {
    $cfg = config();
    if (($cfg['client_id'] ?? '') === '' || ($cfg['client_secret'] ?? '') === '') {
        throw new RuntimeException('Productie-login heeft een geregistreerde client-id en een geheim nodig. Zie README.md.');
    }
    $_SESSION['oauth_state'] = bin2hex(random_bytes(32));
    $_SESSION['oauth_started'] = time();
    header('Location: https://auth.timfalken.com/oauth/authorize?'.http_build_query([
        'client_id' => $cfg['client_id'],
        'redirect_uri' => baseUrl().'/callback.php',
        'response_type' => 'code',
        'state' => $_SESSION['oauth_state'],
    ]));
} catch (Throwable $e) {
    http_response_code(503);
    echo htmlspecialchars($e->getMessage());
}
