import { describe, expect, it } from 'vitest';

import { isCreditsPricing, isSeatsPricing, PricingModelEnum, resolvePricingModel } from './pricingModel.js';

describe('PricingModelEnum', () => {
  it('uses stable string values for the billing.pricing_model column', () => {
    expect(PricingModelEnum.SEATS).toBe('seats');
    expect(PricingModelEnum.CREDITS).toBe('credits');
  });
});

describe('isCreditsPricing', () => {
  it('is true only for credits-based orgs', () => {
    expect(isCreditsPricing(PricingModelEnum.CREDITS)).toBe(true);
  });

  it('is false for seat-based orgs', () => {
    expect(isCreditsPricing(PricingModelEnum.SEATS)).toBe(false);
  });

  it('defaults to false (seat-based) when the model is absent, so existing orgs are grandfathered', () => {
    expect(isCreditsPricing(undefined)).toBe(false);
    expect(isCreditsPricing(null)).toBe(false);
  });
});

describe('isSeatsPricing', () => {
  it('is true for seat-based orgs', () => {
    expect(isSeatsPricing(PricingModelEnum.SEATS)).toBe(true);
  });

  it('is false for credits-based orgs', () => {
    expect(isSeatsPricing(PricingModelEnum.CREDITS)).toBe(false);
  });

  it('treats an absent model as seat-based, so existing orgs are grandfathered', () => {
    expect(isSeatsPricing(undefined)).toBe(true);
    expect(isSeatsPricing(null)).toBe(true);
  });
});

describe('resolvePricingModel', () => {
  it('passes through recognized model values from the LD flag', () => {
    expect(resolvePricingModel('credits')).toBe(PricingModelEnum.CREDITS);
    expect(resolvePricingModel('seats')).toBe(PricingModelEnum.SEATS);
  });

  it('fails closed to seats for an unrecognized flag value (LD typo / unknown variation)', () => {
    expect(resolvePricingModel('gau')).toBe(PricingModelEnum.SEATS);
    expect(resolvePricingModel('Credits')).toBe(PricingModelEnum.SEATS);
    expect(resolvePricingModel('')).toBe(PricingModelEnum.SEATS);
  });

  it('fails closed to seats when the flag is missing or the wrong type', () => {
    expect(resolvePricingModel(undefined)).toBe(PricingModelEnum.SEATS);
    expect(resolvePricingModel(null)).toBe(PricingModelEnum.SEATS);
    expect(resolvePricingModel(true)).toBe(PricingModelEnum.SEATS);
    expect(resolvePricingModel(42)).toBe(PricingModelEnum.SEATS);
  });
});
