import { Input, InputType } from '../../types';
import { dateRangeFilterInputMapping } from './date-range-filter-input.mapping';
import * as schemaModule from '../schemas/date-range-filter-input.schema';

describe('dateRangeFilterInputMapping', () => {
  const mockSchemaParser = jest.fn().mockImplementation((input) => input);

  beforeEach(() => {
    jest.clearAllMocks();

    // Mock the schema parse method to avoid validation errors in tests
    jest
      .spyOn(schemaModule.DateRangeFilterInputSchema, 'parse')
      .mockImplementation(mockSchemaParser);
  });

  it('should map minimal input correctly', () => {
    // Arrange
    const group = 'testGroup';
    const input: Input = {
      key: 'testKey',
      label: 'Test Label',
      type: InputType.DateRange,
    };

    // Act
    const result = dateRangeFilterInputMapping(group, input);

    // Assert
    expect(mockSchemaParser).toHaveBeenCalledWith({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: undefined,
      target: undefined,
      type: InputType.DateRange,
      start: undefined,
      stop: undefined,
    });

    expect(result).toEqual({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: undefined,
      target: undefined,
      type: InputType.DateRange,
      start: undefined,
      stop: undefined,
    });
  });

  it('should map complete input correctly', () => {
    // Arrange
    const group = 'testGroup';
    const input: Input = {
      key: 'testKey',
      label: 'Test Label',
      description: 'Test Description',
      type: InputType.DateRange,
      options: {
        values: [
          { label: 'Start', value: '2023-01-01' },
          { label: 'End', value: '2023-12-31' },
        ],
      },
    };

    // Act
    const result = dateRangeFilterInputMapping(group, input);

    // Assert
    expect(mockSchemaParser).toHaveBeenCalledWith({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: 'Test Description',
      target: undefined,
      type: InputType.DateRange,
      start: '2023-01-01',
      stop: '2023-12-31',
    });

    expect(result).toEqual({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: 'Test Description',
      target: undefined,
      type: InputType.DateRange,
      start: '2023-01-01',
      stop: '2023-12-31',
    });
  });

  it('should handle missing values in options', () => {
    // Arrange
    const group = 'testGroup';
    const input: Input = {
      key: 'testKey',
      label: 'Test Label',
      type: InputType.DateRange,
      options: {
        values: [],
      },
    };

    // Act
    const result = dateRangeFilterInputMapping(group, input);

    // Assert
    expect(mockSchemaParser).toHaveBeenCalledWith({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: undefined,
      target: undefined,
      type: InputType.DateRange,
      start: undefined,
      stop: undefined,
    });

    expect(result).toEqual({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: undefined,
      target: undefined,
      type: InputType.DateRange,
      start: undefined,
      stop: undefined,
    });
  });

  it('should map min and max values correctly', () => {
    // Arrange
    const group = 'testGroup';
    const input: Input = {
      key: 'testKey',
      label: 'Test Label',
      type: InputType.DateRange,
      options: {
        values: [
          {
            label: 'Start',
            value: '2023-01-01',
            minValue: '2022-01-01',
            maxValue: '2024-12-31',
          },
          { label: 'End', value: '2023-12-31' },
        ],
      },
    };

    // Act
    const result = dateRangeFilterInputMapping(group, input);

    // Assert
    expect(mockSchemaParser).toHaveBeenCalledWith({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: undefined,
      target: undefined,
      type: InputType.DateRange,
      start: '2023-01-01',
      stop: '2023-12-31',
      minStart: '2022-01-01',
      maxStop: '2024-12-31',
    });

    expect(result).toEqual({
      group: 'testGroup',
      key: 'testKey',
      label: 'Test Label',
      description: undefined,
      target: undefined,
      type: InputType.DateRange,
      start: '2023-01-01',
      stop: '2023-12-31',
      minStart: '2022-01-01',
      maxStop: '2024-12-31',
    });
  });
});
