import { describe, expect, it } from 'vitest';
import {
  collectPackageFeatures,
  formatEventUsage,
  formatPackageLimit,
  getPackageFeatureLabel,
  getPackageLimitEntries,
} from './packageSummary';

const t = (key: string, options?: Record<string, unknown> | string) => {
  if (typeof options === 'string') {
    return options;
  }
  const template = (options?.defaultValue as string | undefined) ?? key;
  return template
    .replace('{{used}}', String(options?.used ?? '{{used}}'))
    .replace('{{limit}}', String(options?.limit ?? '{{limit}}'))
    .replace('{{remaining}}', String(options?.remaining ?? '{{remaining}}'))
    .replace('{{count}}', String(options?.count ?? '{{count}}'));
};

describe('packageSummary helpers', () => {
  it('returns translated labels for known features', () => {
    expect(getPackageFeatureLabel('priority_support', t)).toBe('Priority support');
  });

  it('falls back to raw feature key for unknown features', () => {
    expect(getPackageFeatureLabel('custom_feature', t)).toBe('custom_feature');
  });

  it('formats unlimited package limits', () => {
    expect(formatPackageLimit(null, t)).toBe('Unlimited');
  });

  it('formats numeric package limits', () => {
    expect(formatPackageLimit(12, t)).toBe('12');
  });

  it('collects features from package and limit payloads', () => {
    const result = collectPackageFeatures({
      features: ['custom_branding'],
      package_limits: { features: ['reseller_dashboard'] },
      branding_allowed: true,
      watermark_allowed: false,
    } as any);

    expect(result).toEqual(expect.arrayContaining(['custom_branding', 'reseller_dashboard', 'branding_allowed']));
  });

  it('returns labeled limit entries', () => {
    const result = getPackageLimitEntries({ max_photos: 120, remaining_photos: 30 }, t);

    expect(result[0].label).toBe('Photos');
    expect(result[0].value).toBe('30 of 120 remaining');
  });

  it('falls back to remaining count when remaining exceeds limit', () => {
    const result = getPackageLimitEntries({ max_photos: 120, remaining_photos: 180 }, t);

    expect(result[0].value).toBe('Remaining 180');
  });

  it('formats event usage copy', () => {
    const result = formatEventUsage(3, 10, t);

    expect(result).toBe('3 of 10 events created');
  });
});
