import { DosageFrequency } from '../enums/regimen.enums.js';

/**
 * Returns the daily frequency multiplier for a given dosage frequency.
 * For example, TWICE_DAILY returns 2, WEEKLY returns 1/7.
 * AS_NEEDED returns 0 (excluded from calculations).
 */
export function getFrequencyMultiplier(frequency: DosageFrequency, customPerDay?: number): number {
  switch (frequency) {
    case DosageFrequency.DAILY:
      return 1;
    case DosageFrequency.TWICE_DAILY:
      return 2;
    case DosageFrequency.THREE_TIMES_DAILY:
      return 3;
    case DosageFrequency.WEEKLY:
      return 1 / 7;
    case DosageFrequency.EVERY_OTHER_DAY:
      return 1 / 2;
    case DosageFrequency.AS_NEEDED:
      return 0;
    case DosageFrequency.CUSTOM:
      return customPerDay ?? 0;
  }
}

/**
 * Calculate total quantity needed for N days based on dosage and frequency.
 *
 * daily:             dosage * numberOfDays
 * twice_daily:       dosage * 2 * numberOfDays
 * three_times_daily: dosage * 3 * numberOfDays
 * weekly:            dosage * ceil(numberOfDays / 7)
 * every_other_day:   dosage * ceil(numberOfDays / 2)
 * as_needed:         0 (excluded from organizer fills)
 * custom:            dosage * customFrequencyPerDay * numberOfDays
 */
export function calculateQuantityNeeded(
  dosage: number,
  frequency: DosageFrequency,
  numberOfDays: number,
  customFrequencyPerDay?: number,
): number {
  switch (frequency) {
    case DosageFrequency.DAILY:
      return dosage * numberOfDays;
    case DosageFrequency.TWICE_DAILY:
      return dosage * 2 * numberOfDays;
    case DosageFrequency.THREE_TIMES_DAILY:
      return dosage * 3 * numberOfDays;
    case DosageFrequency.WEEKLY:
      return dosage * Math.ceil(numberOfDays / 7);
    case DosageFrequency.EVERY_OTHER_DAY:
      return dosage * Math.ceil(numberOfDays / 2);
    case DosageFrequency.AS_NEEDED:
      return 0;
    case DosageFrequency.CUSTOM:
      return dosage * (customFrequencyPerDay ?? 0) * numberOfDays;
  }
}
