'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');

const {
  billingCycleForCycleType,
  cycleTypeFromBillingCycle,
  validateBillData,
} = require('../services/billsService');

test('billing schedule helpers map canonical cycle_type to legacy billing_cycle', () => {
  assert.equal(billingCycleForCycleType('monthly'), 'monthly');
  assert.equal(billingCycleForCycleType('quarterly'), 'quarterly');
  assert.equal(billingCycleForCycleType('annual'), 'annually');
  assert.equal(billingCycleForCycleType('weekly'), 'irregular');
  assert.equal(billingCycleForCycleType('biweekly'), 'irregular');
});

test('billing schedule helpers recover legacy billing_cycle values', () => {
  assert.equal(cycleTypeFromBillingCycle('monthly'), 'monthly');
  assert.equal(cycleTypeFromBillingCycle('quarterly'), 'quarterly');
  assert.equal(cycleTypeFromBillingCycle('annually'), 'annual');
  assert.equal(cycleTypeFromBillingCycle('irregular'), 'monthly');
});

test('validateBillData derives billing_cycle from cycle_type', () => {
  const { errors, normalized } = validateBillData({
    name: 'Gym',
    due_day: 12,
    expected_amount: 25,
    billing_cycle: 'monthly',
    cycle_type: 'biweekly',
    cycle_day: 'friday',
  });

  assert.deepEqual(errors, []);
  assert.equal(normalized.cycle_type, 'biweekly');
  assert.equal(normalized.cycle_day, 'friday');
  assert.equal(normalized.billing_cycle, 'irregular');
});

test('validateBillData uses legacy billing_cycle when cycle_type is absent', () => {
  const { errors, normalized } = validateBillData({
    name: 'Insurance',
    due_day: 20,
    expected_amount: 100,
    billing_cycle: 'annually',
  });

  assert.deepEqual(errors, []);
  assert.equal(normalized.cycle_type, 'annual');
  assert.equal(normalized.billing_cycle, 'annually');
});
