// Controlled reproduction of Vaaya's actual spending decision.
// Run: node spending-check-demo.mjs
// Fixture values only. No account, provider, ledger, or HTTP request.
// Source commit: 71dd3628af0e67554b39afb943359204bae85980
// Extracted TypeScript SHA-256: d5a41da09d28836384d70727166963f8ea8e22ec7d0c67e6a2cafa2d45c1385d
// Extracted JavaScript SHA-256: c523086ff8904ab5290ff9b8c9f4e7e63894016749d261eb0f124e3855688f3f
import assert from 'node:assert/strict';

export const CARDLESS_LINE_CAP_CENTS = 1_000;
export const CARD_PATH = '/card';
export function cardDollars(cents) {
    const decimals = cents % 100 === 0 ? 0 : 2;
    return `$${(cents / 100).toLocaleString('en-US', {
        minimumFractionDigits: decimals,
        maximumFractionDigits: decimals,
    })}`;
}
export function cardRequiredMessage(cardUrl, remainingLineCents) {
    return `You've used ${cardDollars(CARDLESS_LINE_CAP_CENTS)} of your Vaaya credit line (${cardDollars(Math.max(0, remainingLineCents))} left). Add a card to keep going: ${cardUrl}. Nothing is charged today; the card settles what you use, weekly.`;
}
export const CREDIT_PACK_CENTS = [1000, 3000, 10000];
export function evaluateSpend(input) {
    const creditLine = input.creditLineCents ?? 0;
    if (input.balanceCents + creditLine - input.priceCents >= 0)
        return { ok: true };
    const withCard = input.withCardLineCents ?? creditLine;
    if (withCard > creditLine && input.balanceCents + withCard - input.priceCents >= 0) {
        const cardUrl = input.cardUrl ?? CARD_PATH;
        const remainingLineCents = input.balanceCents + withCard;
        return {
            ok: false,
            code: 'card_required',
            message: cardRequiredMessage(cardUrl, remainingLineCents),
            creditsUrl: input.creditsUrl,
            cardUrl,
            remainingLineCents,
        };
    }
    const packs = CREDIT_PACK_CENTS.map((c) => `$${c / 100}`).join(' / ');
    return {
        ok: false,
        code: 'credits_required',
        message: creditLine > 0
            ? `You're out of credits and your credit line is fully drawn. Buy a credit pack (${packs}) at ${input.creditsUrl} to keep going.`
            : `You're out of credits. Buy a credit pack (${packs}) at ${input.creditsUrl} to keep going.`,
        creditsUrl: input.creditsUrl,
    };
}

const fixtures = [
  { name: 'empty_available_credit', input: { balanceCents: 0, creditLineCents: 0, withCardLineCents: 0, priceCents: 1, creditsUrl: 'https://vaaya.ai/balance' }, expected: { ok: false, code: 'credits_required' } },
  { name: 'after_synthetic_one_cent_funding', input: { balanceCents: 1, creditLineCents: 0, withCardLineCents: 0, priceCents: 1, creditsUrl: 'https://vaaya.ai/balance' }, expected: { ok: true } }
];
const cases = fixtures.map(({ name, input, expected }) => {
  const result = evaluateSpend(input);
  assert.equal(result.ok, expected.ok);
  if ('code' in expected) assert.equal(result.code, expected.code);
  return { name, fixture: input, result, assertion: 'passed' };
});
process.stdout.write(JSON.stringify({
  artifact_type: 'controlled_offline_test',
  executed_at: new Date().toISOString(),
  source_commit: '71dd3628af0e67554b39afb943359204bae85980',
  source_function: 'evaluateSpend',
  extracted_typescript_sha256: 'd5a41da09d28836384d70727166963f8ea8e22ec7d0c67e6a2cafa2d45c1385d',
  description: 'Actual extracted spending-decision function executed with synthetic balances. This is not a live purchase, HTTP response, ledger transaction or real top-up.',
  limitations: 'The pure function returns only a decision. HTTP status, provider-call prevention, and ledger effects are covered separately by dispatcher tests.',
  cases
}, null, 2) + '\n');
