import { describe, it, expect } from 'vitest';
import { groupDisplayOrdersByDeliveryType } from './group-display-orders-by-delivery-type.helper';
import { DisplayOrder } from '@isa/oms/data-access';

describe('groupDisplayOrdersByDeliveryType', () => {
  it('should group orders by delivery type', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        features: { orderType: 'Versand' },
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        features: { orderType: 'Abholung' },
        orderType: { id: 2 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 3,
        features: { orderType: 'Versand' },
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 4,
        features: { orderType: 'Rücklage' },
        orderType: { id: 3 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByDeliveryType(orders);

    expect(result.size).toBe(3);
    expect(result.get('Versand')).toHaveLength(2);
    expect(result.get('Abholung')).toHaveLength(1);
    expect(result.get('Rücklage')).toHaveLength(1);
  });

  it('should handle orders without order type feature', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        features: {},
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        features: undefined,
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByDeliveryType(orders);

    expect(result.size).toBe(1);
    expect(result.has('Unbekannt')).toBe(true);
    expect(result.get('Unbekannt')).toHaveLength(2);
  });

  it('should return empty map for empty orders array', () => {
    const result = groupDisplayOrdersByDeliveryType([]);

    expect(result.size).toBe(0);
  });

  it('should handle single order', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        features: { orderType: 'Abholung' },
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByDeliveryType(orders);

    expect(result.size).toBe(1);
    expect(result.get('Abholung')).toHaveLength(1);
    expect(result.get('Abholung')?.[0]?.id).toBe(1);
  });

  it('should handle all orders with same order type', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        features: { orderType: 'Versand' },
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        features: { orderType: 'Versand' },
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 3,
        features: { orderType: 'Versand' },
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByDeliveryType(orders);

    expect(result.size).toBe(1);
    expect(result.get('Versand')).toHaveLength(3);
  });

  it('should handle all supported order types', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        features: { orderType: 'Abholung' },
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        features: { orderType: 'Rücklage' },
        orderType: { id: 2 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 3,
        features: { orderType: 'Versand' },
        orderType: { id: 3 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 4,
        features: { orderType: 'DIG-Versand' },
        orderType: { id: 4 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 5,
        features: { orderType: 'B2B-Versand' },
        orderType: { id: 5 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 6,
        features: { orderType: 'Download' },
        orderType: { id: 6 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByDeliveryType(orders);

    expect(result.size).toBe(6);
    expect(result.has('Abholung')).toBe(true);
    expect(result.has('Rücklage')).toBe(true);
    expect(result.has('Versand')).toBe(true);
    expect(result.has('DIG-Versand')).toBe(true);
    expect(result.has('B2B-Versand')).toBe(true);
    expect(result.has('Download')).toBe(true);
  });
});
