import {
  createDirectiveFactory,
  SpectatorDirective,
} from '@ngneat/spectator/jest';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { CalendarBodyCellDirective } from './calendar-body-cell.directive';
import { startOfDay } from 'date-fns';
import { RangeDatepicker } from '../../range-datepicker';

describe('CalendarBodyCellDirective', () => {
  let spectator: SpectatorDirective<CalendarBodyCellDirective>;
  const today = new Date();
  const todayStart = startOfDay(today);
  const dayBeforeYesterday = new Date(
    todayStart.getFullYear(),
    todayStart.getMonth(),
    todayStart.getDate() - 2,
  );

  const datepickerMock = {
    value: jest.fn(),
    min: jest.fn(),
    max: jest.fn(),
    range: jest.fn(),
    setDate: jest.fn(),
    setDateRange: jest.fn(),
  };

  const createDirective = createDirectiveFactory({
    directive: CalendarBodyCellDirective,
    providers: [{ provide: RangeDatepicker, useValue: datepickerMock }],
    schemas: [NO_ERRORS_SCHEMA],
  });

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

  it('should mark isToday true when cell date equals today', () => {
    spectator = createDirective(
      `<div uiCalendarBodyCell="${todayStart.toISOString()}"></div>`,
    );
    expect(spectator.directive.isToday()).toBe(true);
  });

  it('should mark isToday false when cell date is not today', () => {
    spectator = createDirective(
      `<div uiCalendarBodyCell="${dayBeforeYesterday.toISOString()}"></div>`,
    );
    expect(spectator.directive.isToday()).toBe(false);
  });

  it('should return false for isDisabled when cell date is within min and max', () => {
    spectator = createDirective(
      `<div uiCalendarBodyCell="${todayStart.toISOString()}"></div>`,
    );
    datepickerMock.min.mockReturnValue(undefined);
    datepickerMock.max.mockReturnValue(undefined);
    spectator.detectChanges();
    expect(spectator.directive.isDisabled()).toBe(false);
  });
});
