import { formatResponse, handleError } from './shared.js';
import { getLicenseGate } from '../services/license/index.js';
async function handleLicenseStatus() {
    try {
        const gate = getLicenseGate();
        const status = gate.getStatus();
        const pending = gate.getPendingInfo();
        const hasKey = gate.hasLicenseKey();
        const serverUrl = gate.getServerUrl();
        const dashboardBase = serverUrl.replace(/\/v1\/?$/, '').replace(/\/$/, '');
        const addFundsUrl = `${dashboardBase}/dashboard`;
        const response = {
            billing_enabled: true,
            balance_cents: status.balance_cents,
            balance_display: `$${(status.balance_cents / 100).toFixed(2)}`,
            files_remaining: status.files_remaining,
            cost_per_file_cents: status.cost_per_file_cents,
            pending_charges: pending.pending_count,
            pending_amount_cents: pending.pending_amount_cents,
            has_license_key: hasKey,
            add_funds_url: addFundsUrl,
        };
        if (status.balance_cents === 0) {
            response.message = 'Balance is $0.00. Add funds to process files.';
            response.action_required = 'Add funds at ' + addFundsUrl;
        }
        // Fetch spending limit info from server (non-blocking — omit on failure)
        const spending = await gate.getSpendingInfo();
        if (spending) {
            response.spending_limit = {
                enabled: spending.spending_limit_cents !== null,
                limit_cents: spending.spending_limit_cents,
                limit_display: spending.spending_limit_cents !== null
                    ? `$${(spending.spending_limit_cents / 100).toFixed(2)}/month`
                    : 'No limit',
                spent_this_month_cents: spending.spent_this_month_cents,
                spent_display: `$${(spending.spent_this_month_cents / 100).toFixed(2)}`,
                remaining_cents: spending.spending_limit_cents !== null
                    ? Math.max(0, spending.spending_limit_cents - spending.spent_this_month_cents)
                    : null,
                resets_at: spending.spending_limit_resets_at,
            };
        }
        return formatResponse(response);
    }
    catch (error) {
        return handleError(error);
    }
}
export const licenseTools = {
    ocr_license_status: {
        description: 'Check your license status including balance, files remaining, and pending charges. ' +
            'Returns billing status, current balance, cost per file, and a link to add funds.',
        inputSchema: {},
        handler: handleLicenseStatus,
    },
};
//# sourceMappingURL=license.js.map