<?php
/**
 * GET /api/dashboard/corpus-init.php
 *
 * Idempotent: ensures the current session has a CaveauAI client tenant +
 * default corpus, lazy-creating both on first hit. Safe to call on every
 * dashboard page load (results are session-cached).
 *
 * Response:
 *   {
 *     "ok": true,
 *     "client_id": 102,
 *     "client_user_id": 257,
 *     "corpus_id": 18,
 *     "created": false
 *   }
 */

declare(strict_types=1);

require_once dirname(__DIR__, 2) . '/includes/bootstrap.php';

dbnToolsRequireMethod('GET');
dbnToolsRequireAuth();

try {
    $tenant = dbnToolsEnsureDashboardTenant();
} catch (DbnToolsHttpException $e) {
    dbnToolsError($e->getMessage(), $e->status, $e->errorCode, $e->extra);
}

dbnToolsRespond([
    'ok'             => true,
    'client_id'      => (int)$tenant['client_id'],
    'client_user_id' => (int)$tenant['client_user_id'],
    'corpus_id'      => (int)$tenant['corpus_id'],
    'created'        => (bool)($tenant['created'] ?? false),
]);
