import { getItemType } from './get-item-type.helper';
import { RemissionItemType } from '../models';
import { RemissionItem } from '../stores';

describe('getItemType', () => {
  describe('Happy Path - ReturnItem', () => {
    it('should return ReturnItem when item has predefinedReturnQuantity', () => {
      // Arrange
      const item = {
        id: 123,
        product: {
          name: 'Test Product',
          ean: '1234567890123',
        },
        retailPrice: {
          value: {
            value: 10.99,
            currency: 'EUR',
          },
        },
        source: 'manually-added',
        predefinedReturnQuantity: 1,
        quantity: 1,
      } as RemissionItem;

      // Act
      const result = getItemType(item);

      // Assert
      expect(result).toBe(RemissionItemType.ReturnItem);
    });

    it('should return ReturnItem when item has no suggestion-specific fields', () => {
      // Arrange
      const item = {
        id: 456,
        product: {
          name: 'Another Product',
          ean: '9876543210987',
        },
        retailPrice: {
          value: {
            value: 15.5,
            currency: 'EUR',
          },
        },
        source: 'DisposalListModule',
        returnReason: 'Herstellerfehler',
      } as RemissionItem;

      // Act
      const result = getItemType(item);

      // Assert
      expect(result).toBe(RemissionItemType.ReturnItem);
    });
  });

  describe('Happy Path - ReturnSuggestion', () => {
    it('should return ReturnSuggestion when item has sort field', () => {
      // Arrange
      const item = {
        id: 789,
        product: {
          name: 'Suggestion Product',
          ean: '5555555555555',
          contributors: 'Test Author',
          format: 'TB',
          formatDetail: 'Taschenbuch',
        },
        retailPrice: {
          value: {
            value: 20.0,
            currency: 'EUR',
          },
        },
        source: 'manually-added',
        quantity: 3,
        sort: 1,
      } as RemissionItem;

      // Act
      const result = getItemType(item);

      // Assert
      expect(result).toBe(RemissionItemType.ReturnSuggestion);
    });

    it('should return ReturnSuggestion when item has accepted field', () => {
      // Arrange
      const item = {
        id: 101,
        product: {
          name: 'Accepted Product',
          ean: '1111111111111',
          contributors: 'Test Author',
          format: 'TB',
          formatDetail: 'Taschenbuch',
        },
        retailPrice: {
          value: {
            value: 8.99,
            currency: 'EUR',
          },
        },
        source: 'DisposalListModule',
        quantity: 2,
        accepted: '2025-10-20T10:00:00Z',
      } as RemissionItem;

      // Act
      const result = getItemType(item);

      // Assert
      expect(result).toBe(RemissionItemType.ReturnSuggestion);
    });
  });

  describe('Fallback with RemissionListType', () => {
    it('should return ReturnItem when neither guard matches and remissionListType is Pflichtremission', () => {
      // Arrange - Item that doesn't match either guard (missing required fields)
      const ambiguousItem = {
        id: 202,
        product: {
          name: 'Ambiguous Product',
          ean: '1234567890',
          contributors: 'Test',
          format: 'TB',
          formatDetail: 'Taschenbuch',
        },
        retailPrice: {
          value: {
            value: 12.5,
            currency: 'EUR',
          },
        },
        // Missing 'source' field - will fail both guards
        quantity: 1,
      } as unknown as RemissionItem;

      // Act
      const result = getItemType(ambiguousItem, 'Pflichtremission');

      // Assert
      expect(result).toBe(RemissionItemType.ReturnItem);
    });

    it('should return ReturnSuggestion when neither guard matches and remissionListType is Abteilungsremission', () => {
      // Arrange - Item that doesn't match either guard (missing required fields)
      const ambiguousItem = {
        id: 303,
        product: {
          name: 'Another Ambiguous Product',
          ean: '9876543210',
          contributors: 'Test',
          format: 'TB',
          formatDetail: 'Taschenbuch',
        },
        retailPrice: {
          value: {
            value: 7.5,
            currency: 'EUR',
          },
        },
        // Missing 'source' field - will fail both guards
        quantity: 1,
      } as unknown as RemissionItem;

      // Act
      const result = getItemType(ambiguousItem, 'Abteilungsremission');

      // Assert
      expect(result).toBe(RemissionItemType.ReturnSuggestion);
    });
  });

  describe('Unknown Type', () => {
    it('should return Unknown when no guards match and no remissionListType provided', () => {
      // Arrange
      const invalidItem = {
        id: 404,
      } as RemissionItem;

      // Act
      const result = getItemType(invalidItem);

      // Assert
      expect(result).toBe(RemissionItemType.Unknown);
    });
  });
});
