require('../config/loadEnv')();
const assert = require('assert');
const service = require('../services/canonicalPointConversionService');

const valid = service.normalizeRequest({
  userId: 'test-user',
  idempotencyKey: 'POINT_CONVERSION:test-user:self-test',
  pointAmount: 3000,
  coinAmount: 3,
  conversionRate: 1000
});
assert.strictEqual(valid.pointAmount, 3000);
assert.strictEqual(valid.coinAmount, 3);
assert.strictEqual(valid.rate, 1000);

assert.throws(() => service.normalizeRequest({
  userId: 'test-user',
  idempotencyKey: 'POINT_CONVERSION:test-user:bad-rate',
  pointAmount: 3000,
  coinAmount: 30,
  conversionRate: 100
}), error => error.code === 'POINT_CONVERSION_RATE_MISMATCH');

assert.throws(() => service.normalizeRequest({
  userId: 'test-user',
  idempotencyKey: 'POINT_CONVERSION:test-user:fraction',
  pointAmount: 1500,
  coinAmount: 1.5,
  conversionRate: 1000
}), error => error.code === 'POINT_CONVERSION_AMOUNT_INVALID');

const readiness = service.getReadiness('self-test-owner');
assert.strictEqual(readiness.rate, 1000);
assert.strictEqual(readiness.mode, 'canonical-besu-qbft');
assert.ok(Array.isArray(readiness.checks));

process.stdout.write(`${JSON.stringify({
  ok: true,
  schemaVersion: 'plais-canonical-point-conversion-self-test/v1',
  readiness: readiness.ready,
  failedChecks: readiness.checks.filter(check => !check.ok).map(check => check.key)
})}\n`);
