import { describe, it, expect } from 'vitest';
import { getPrimaryBonusCard } from './get-primary-bonus-card.helper';
import { BonusCardInfo } from '../models';

describe('getPrimaryBonusCard', () => {
  it('should return the primary bonus card when one exists', () => {
    // Arrange
    const bonusCards: BonusCardInfo[] = [
      {
        code: 'CARD-B',
        firstName: 'John',
        lastName: 'Doe',
        isActive: true,
        isPrimary: false,
        totalPoints: 100,
      } as BonusCardInfo,
      {
        code: 'CARD-A',
        firstName: 'Jane',
        lastName: 'Smith',
        isActive: true,
        isPrimary: true,
        totalPoints: 200,
      } as BonusCardInfo,
    ];

    // Act
    const result = getPrimaryBonusCard(bonusCards);

    // Assert
    expect(result).toBeDefined();
    expect(result?.isPrimary).toBe(true);
    expect(result?.code).toBe('CARD-A');
  });

  it('should return first alphabetically when no primary card exists', () => {
    // Arrange
    const bonusCards: BonusCardInfo[] = [
      {
        code: 'CARD-C',
        firstName: 'John',
        lastName: 'Doe',
        isActive: true,
        isPrimary: false,
        totalPoints: 100,
      } as BonusCardInfo,
      {
        code: 'CARD-A',
        firstName: 'Jane',
        lastName: 'Smith',
        isActive: true,
        isPrimary: false,
        totalPoints: 200,
      } as BonusCardInfo,
    ];

    // Act
    const result = getPrimaryBonusCard(bonusCards);

    // Assert
    expect(result).toBeDefined();
    expect(result?.code).toBe('CARD-A');
  });

  it('should return undefined when bonus cards array is empty', () => {
    // Arrange
    const bonusCards: BonusCardInfo[] = [];

    // Act
    const result = getPrimaryBonusCard(bonusCards);

    // Assert
    expect(result).toBeUndefined();
  });

  it('should return first alphabetically when multiple primary cards exist', () => {
    // Arrange
    const bonusCards: BonusCardInfo[] = [
      {
        code: 'CARD-Z',
        firstName: 'John',
        lastName: 'Doe',
        isActive: true,
        isPrimary: true,
        totalPoints: 100,
      } as BonusCardInfo,
      {
        code: 'CARD-A',
        firstName: 'Jane',
        lastName: 'Smith',
        isActive: true,
        isPrimary: true,
        totalPoints: 200,
      } as BonusCardInfo,
    ];

    // Act
    const result = getPrimaryBonusCard(bonusCards);

    // Assert
    expect(result).toBeDefined();
    expect(result?.isPrimary).toBe(true);
    expect(result?.code).toBe('CARD-A');
  });
});
