import { describe, it, expect, vi, afterEach } from 'vitest'
import { useReplayControls } from '@/composables/useReplayControls'

describe('useReplayControls', () => {
  afterEach(() => {
    vi.useRealTimers()
  })

  it('pauseAndResume', () => {
    const replay = useReplayControls()
    replay.setScenarioBounds(0, 60_000)
    replay.syncToEnd()

    expect(replay.isPaused.value).toBe(true)
    replay.resume()
    expect(replay.isPaused.value).toBe(false)
    replay.pause()
    expect(replay.isPaused.value).toBe(true)
  })

  it('speedPresets', () => {
    const { speed, setSpeed } = useReplayControls()
    expect(speed.value).toBe(60)
    setSpeed(360)
    expect(speed.value).toBe(360)
  })

  it('progressComputation', () => {
    const replay = useReplayControls()
    replay.setScenarioBounds(0, 100 * 60_000)
    replay.jumpToOffset(50)
    expect(replay.progress.value).toBe(50)
  })

  it('jumpToOffset', () => {
    const replay = useReplayControls()
    replay.setScenarioBounds(0, 100 * 60_000)
    replay.jumpToOffset(42)
    expect(replay.currentOffsetMinutes.value).toBe(42)
  })

  it('jumpToTimestamp_clampsWithinScenario', () => {
    const replay = useReplayControls()
    const start = new Date('2026-06-19T12:00:00Z').getTime()
    const end = new Date('2026-06-19T14:00:00Z').getTime()
    replay.setScenarioBounds(start, end)
    replay.jumpToTimestamp('2026-06-19T13:00:00Z')
    expect(replay.currentOffsetMinutes.value).toBe(60)
  })

  it('isAtOrBefore_filtersByCurrentTime', () => {
    const replay = useReplayControls()
    replay.setScenarioBounds(0, 120 * 60_000)
    replay.jumpToOffset(30)
    expect(replay.isAtOrBefore(new Date(20 * 60_000).toISOString())).toBe(true)
    expect(replay.isAtOrBefore(new Date(40 * 60_000).toISOString())).toBe(false)
  })

  it('instantSpeedJumpsToEnd', () => {
    const replay = useReplayControls()
    replay.setScenarioBounds(0, 60_000)
    replay.jumpToOffset(10)
    replay.setSpeed(0)
    expect(replay.progress.value).toBe(100)
    expect(replay.isPaused.value).toBe(true)
  })
})
