<?php
/**
 * Shared bootstrap for finance AJAX endpoints.
 * Enforces auth + finance_suite license, exposes $conn and $actor.
 */
if (session_status() === PHP_SESSION_NONE) session_start();
header('Content-Type: application/json');

require_once __DIR__ . '/../../config.php';

if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Not signed in.']);
    exit;
}

requireModule('finance_suite', true);   // 403 JSON if unlicensed / role-blocked

require_once __DIR__ . '/../inc/finance_lib.php';

$conn = getDBConnection();
finance_ensure_tables($conn);
$actor = finance_actor();

/** Read a request field from POST or GET. */
function fin_in(string $key, $default = null) {
    return $_POST[$key] ?? $_GET[$key] ?? $default;
}

/** JSON reply + exit. */
function fin_out(array $payload): void {
    echo json_encode($payload);
    exit;
}
