import { isEventPast } from 'src/lib/events'

const HOUR_MS = 60 * 60 * 1000

function hoursFromNow(h: number): string {
  return new Date(Date.now() + h * HOUR_MS).toISOString()
}

describe('isEventPast', () => {
  test('returns true when end_time is in the past', () => {
    expect(
      isEventPast({
        start_time: hoursFromNow(-3),
        end_time: hoursFromNow(-1),
        is_open_ended: false,
      })
    ).toBe(true)
  })

  test('returns false when end_time is in the future', () => {
    expect(
      isEventPast({
        start_time: hoursFromNow(-1),
        end_time: hoursFromNow(1),
        is_open_ended: false,
      })
    ).toBe(false)
  })

  test('falls back to start_time when end_time is null and start is in the past', () => {
    expect(
      isEventPast({
        start_time: hoursFromNow(-2),
        end_time: null,
        is_open_ended: false,
      })
    ).toBe(true)
  })

  test('falls back to start_time when end_time is null and start is in the future', () => {
    expect(
      isEventPast({
        start_time: hoursFromNow(2),
        end_time: null,
        is_open_ended: false,
      })
    ).toBe(false)
  })

  test('open-ended events are never past, even with a past end_time', () => {
    expect(
      isEventPast({
        start_time: hoursFromNow(-3),
        end_time: hoursFromNow(-1),
        is_open_ended: true,
      })
    ).toBe(false)
  })

  test('unparseable date strings return false (do not gate on bad data)', () => {
    expect(
      isEventPast({
        start_time: 'not-a-date',
        end_time: 'also-not-a-date',
        is_open_ended: false,
      })
    ).toBe(false)
  })

  test('returns false when both dates are missing', () => {
    expect(
      isEventPast({
        start_time: '',
        end_time: null,
        is_open_ended: false,
      })
    ).toBe(false)
  })
})
