import { canReturnReceiptItem } from './can-return-receipt-item.helper';
import { ReceiptItem } from '../../models/receipt-item';
import { Product } from '../../schemas/product.schema';
import { Quantity } from '../../models/quantity';

describe('canReturnReceiptItem', () => {
  const product: Product = {
    name: 'Test Product',
    contributors: 'Author',
    catalogProductNumber: '123',
    ean: '1234567890123',
    format: 'Hardcover',
    formatDetail: 'Detail',
    volume: '1',
    manufacturer: 'Test Publisher',
  };
  const quantity: Quantity = { quantity: 1 };

  const baseItem: ReceiptItem = {
    id: 1,
    product,
    quantity,
    receiptNumber: 'R-001',
    actions: [],
  };

  it('should return false if actions property is missing', () => {
    const item = { ...baseItem };
    delete (item as { actions?: unknown }).actions;
    expect(canReturnReceiptItem(item as ReceiptItem)).toBe(false);
  });

  it('should return false if canReturn action is missing', () => {
    const item = { ...baseItem, actions: [{ key: 'other', value: 'true' }] };
    expect(canReturnReceiptItem(item)).toBe(false);
  });

  it('should return false if canReturn action value is falsy', () => {
    const item = { ...baseItem, actions: [{ key: 'canReturn', value: '' }] };
    // coerceBooleanProperty('') returns true, so this should be true
    expect(canReturnReceiptItem(item)).toBe(true);
    const itemZero = {
      ...baseItem,
      actions: [{ key: 'canReturn', value: '0' }],
    };
    // coerceBooleanProperty('0') returns true (string '0' is truthy)
    expect(canReturnReceiptItem(itemZero)).toBe(true);
  });

  it('should return true if canReturn action value is truthy', () => {
    const item = {
      ...baseItem,
      actions: [{ key: 'canReturn', value: 'true' }],
    };
    expect(canReturnReceiptItem(item)).toBe(true);
  });

  it('should coerce canReturn action value to boolean', () => {
    const item = { ...baseItem, actions: [{ key: 'canReturn', value: '1' }] };
    expect(canReturnReceiptItem(item)).toBe(true);
    const itemFalse = {
      ...baseItem,
      actions: [{ key: 'canReturn', value: '' }],
    };
    expect(canReturnReceiptItem(itemFalse)).toBe(true);
    const itemStringZero = {
      ...baseItem,
      actions: [{ key: 'canReturn', value: '0' }],
    };
    // coerceBooleanProperty('0') returns true
    expect(canReturnReceiptItem(itemStringZero)).toBe(true);
  });
});
