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

describe('groupDisplayOrdersByBranch', () => {
  it('should not group orders by branch for Versand order type', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        items: [{ id: 1, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        items: [{ id: 2, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('Versand', orders);

    expect(result).toHaveLength(1);
    expect(result[0].branchId).toBeUndefined();
    expect(result[0].branchName).toBeUndefined();
    expect(result[0].orders).toHaveLength(2);
    expect(result[0].allItems).toHaveLength(2);
  });

  it('should group orders by branch for Abholung order type', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        targetBranch: {
          id: 1,
          name: 'Filiale München',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 1, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        targetBranch: {
          id: 2,
          name: 'Filiale Berlin',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 2, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 3,
        targetBranch: {
          id: 1,
          name: 'Filiale München',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 3, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('Abholung', orders);

    expect(result).toHaveLength(2);
    expect(result[0].branchId).toBe(1);
    expect(result[0].branchName).toBe('Filiale München');
    expect(result[0].orders).toHaveLength(2);
    expect(result[0].allItems).toHaveLength(2);
    expect(result[1].branchId).toBe(2);
    expect(result[1].branchName).toBe('Filiale Berlin');
    expect(result[1].orders).toHaveLength(1);
    expect(result[1].allItems).toHaveLength(1);
  });

  it('should group orders by branch for Rücklage order type', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        targetBranch: {
          id: 1,
          name: 'Filiale Hamburg',
          quantityUnitType: { id: 1 },
        },
        items: [
          { id: 1, quantityUnitType: { id: 1 } } as DisplayOrderItem,
          { id: 2, quantityUnitType: { id: 1 } } as DisplayOrderItem,
        ],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('Rücklage', orders);

    expect(result).toHaveLength(1);
    expect(result[0].branchId).toBe(1);
    expect(result[0].branchName).toBe('Filiale Hamburg');
    expect(result[0].orders).toHaveLength(1);
    expect(result[0].allItems).toHaveLength(2);
  });

  it('should sort branch groups by branch ID', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        targetBranch: {
          id: 3,
          name: 'Branch 3',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 1, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        targetBranch: {
          id: 1,
          name: 'Branch 1',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 2, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 3,
        targetBranch: {
          id: 2,
          name: 'Branch 2',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 3, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('Abholung', orders);

    expect(result).toHaveLength(3);
    expect(result[0].branchId).toBe(1);
    expect(result[1].branchId).toBe(2);
    expect(result[2].branchId).toBe(3);
  });

  it('should handle orders without targetBranch for pickup types', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        targetBranch: undefined,
        items: [{ id: 1, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('Abholung', orders);

    expect(result).toHaveLength(1);
    expect(result[0].branchId).toBeUndefined();
    expect(result[0].branchName).toBeUndefined();
    expect(result[0].orders).toHaveLength(1);
  });

  it('should flatten items from multiple orders in same branch', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        targetBranch: {
          id: 1,
          name: 'Branch 1',
          quantityUnitType: { id: 1 },
        },
        items: [
          { id: 1, quantityUnitType: { id: 1 } } as DisplayOrderItem,
          { id: 2, quantityUnitType: { id: 1 } } as DisplayOrderItem,
        ],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        targetBranch: {
          id: 1,
          name: 'Branch 1',
          quantityUnitType: { id: 1 },
        },
        items: [
          { id: 3, quantityUnitType: { id: 1 } } as DisplayOrderItem,
          { id: 4, quantityUnitType: { id: 1 } } as DisplayOrderItem,
          { id: 5, quantityUnitType: { id: 1 } } as DisplayOrderItem,
        ],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('Abholung', orders);

    expect(result).toHaveLength(1);
    expect(result[0].allItems).toHaveLength(5);
    expect(result[0].allItems.map((item) => item.id)).toEqual([1, 2, 3, 4, 5]);
  });

  it('should handle orders without items', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        targetBranch: {
          id: 1,
          name: 'Branch 1',
          quantityUnitType: { id: 1 },
        },
        items: undefined,
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('Abholung', orders);

    expect(result).toHaveLength(1);
    expect(result[0].allItems).toHaveLength(0);
  });

  it('should handle empty orders array', () => {
    const result = groupDisplayOrdersByBranch('Abholung', []);

    expect(result).toHaveLength(0);
  });

  it('should not group DIG-Versand by branch', () => {
    const orders: DisplayOrder[] = [
      {
        id: 1,
        targetBranch: {
          id: 1,
          name: 'Branch 1',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 1, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
      {
        id: 2,
        targetBranch: {
          id: 2,
          name: 'Branch 2',
          quantityUnitType: { id: 1 },
        },
        items: [{ id: 2, quantityUnitType: { id: 1 } } as DisplayOrderItem],
        orderType: { id: 1 },
        quantityUnitType: { id: 1 },
      } as DisplayOrder,
    ];

    const result = groupDisplayOrdersByBranch('DIG-Versand', orders);

    expect(result).toHaveLength(1);
    expect(result[0].branchId).toBeUndefined();
    expect(result[0].orders).toHaveLength(2);
    expect(result[0].allItems).toHaveLength(2);
  });
});
