import {
  calculateStockToRemit,
  getStockToRemit,
} from './calc-stock-to-remit.helper';
import { RemissionListType } from '@isa/remission/data-access';

describe('calculateStockToRemit', () => {
  it('should return predefinedReturnQuantity when provided (even if 0) - #5280 Fix', () => {
    const result = calculateStockToRemit({
      availableStock: 10,
      predefinedReturnQuantity: 0,
      remainingQuantityInStock: 2,
    });

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

  it('should return predefinedReturnQuantity when provided with positive value', () => {
    const result = calculateStockToRemit({
      availableStock: 10,
      predefinedReturnQuantity: 5,
      remainingQuantityInStock: 2,
    });

    expect(result).toBe(5);
  });

  it('should calculate availableStock minus remainingQuantityInStock when no predefinedReturnQuantity - #5269 Fix', () => {
    const result = calculateStockToRemit({
      availableStock: 10,
      remainingQuantityInStock: 3,
    });

    expect(result).toBe(7);
  });

  it('should return 0 when approximation calculation would be negative - #5269 Fix', () => {
    const result = calculateStockToRemit({
      availableStock: 5,
      remainingQuantityInStock: 8,
    });

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

  it('should handle undefined remainingQuantityInStock when no predefinedReturnQuantity - #5269 Fix', () => {
    const result = calculateStockToRemit({
      availableStock: 10,
      remainingQuantityInStock: undefined,
    });

    expect(result).toBe(10);
  });

  it('should handle null remainingQuantityInStock when no predefinedReturnQuantity - #5269 Fix', () => {
    const result = calculateStockToRemit({
      availableStock: 10,
      // @ts-ignore - Testing runtime behavior with null
      remainingQuantityInStock: null,
    });

    expect(result).toBe(10);
  });
});

describe('getStockToRemit', () => {
  it('should handle Pflicht remission list type with predefined return quantity', () => {
    const remissionItem = {
      remainingQuantityInStock: 2,
      predefinedReturnQuantity: 5,
    } as any;

    const result = getStockToRemit({
      remissionItem,
      remissionListType: RemissionListType.Pflicht,
      availableStock: 10,
    });

    expect(result).toBe(5);
  });

  it('should handle Pflicht remission list type with zero predefined return quantity - #5280 Fix', () => {
    const remissionItem = {
      remainingQuantityInStock: 2,
      predefinedReturnQuantity: 0,
    } as any;

    const result = getStockToRemit({
      remissionItem,
      remissionListType: RemissionListType.Pflicht,
      availableStock: 10,
    });

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

  it('should handle Pflicht remission list type without predefined return quantity - #5269 Fix', () => {
    const remissionItem = {
      remainingQuantityInStock: 3,
    } as any;

    const result = getStockToRemit({
      remissionItem,
      remissionListType: RemissionListType.Pflicht,
      availableStock: 10,
    });

    expect(result).toBe(7);
  });

  it('should handle Abteilung remission list type with return suggestion', () => {
    const remissionItem = {
      remainingQuantityInStock: 1,
      returnItem: {
        data: {
          predefinedReturnQuantity: 8,
        },
      },
    } as any;

    const result = getStockToRemit({
      remissionItem,
      remissionListType: RemissionListType.Abteilung,
      availableStock: 10,
    });

    expect(result).toBe(8);
  });

  it('should handle Abteilung remission list type with zero return suggestion - #5280 Fix', () => {
    const remissionItem = {
      remainingQuantityInStock: 1,
      returnItem: {
        data: {
          predefinedReturnQuantity: 0,
        },
      },
    } as any;

    const result = getStockToRemit({
      remissionItem,
      remissionListType: RemissionListType.Abteilung,
      availableStock: 10,
    });

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

  it('should handle Abteilung remission list type without return suggestion - #5269 Fix', () => {
    const remissionItem = {
      remainingQuantityInStock: 2,
      returnItem: {
        data: {},
      },
    } as any;

    const result = getStockToRemit({
      remissionItem,
      remissionListType: RemissionListType.Abteilung,
      availableStock: 10,
    });

    expect(result).toBe(8);
  });

  it('should handle Abteilung remission list type with missing returnItem - #5269 Fix', () => {
    const remissionItem = {
      remainingQuantityInStock: 1,
    } as any;

    const result = getStockToRemit({
      remissionItem,
      remissionListType: RemissionListType.Abteilung,
      availableStock: 10,
    });

    expect(result).toBe(9);
  });
});
