import { describe, it, expect } from 'vitest'
import { formatBucketTime, getTimeRangeLabel, getTimeRangeDates } from '../utils'

describe('formatBucketTime', () => {
  it('formats hour granularity as HH:MM (24h)', () => {
    // 2024-01-15T14:30:00Z => should produce a time string like "14:30" (locale-dependent)
    const result = formatBucketTime('2024-01-15T14:30:00Z', 'hour')
    // Verify it contains a colon (time format) and does not contain a month name
    expect(result).toMatch(/\d{1,2}:\d{2}/)
  })

  it('formats day granularity as "Mon DD"', () => {
    const result = formatBucketTime('2024-01-15T14:30:00Z', 'day')
    // Should contain a short month and day number, e.g. "Jan 15"
    expect(result).toMatch(/[A-Z][a-z]{2}\s+\d{1,2}/)
  })

  it('handles midnight timestamps for hour granularity', () => {
    const result = formatBucketTime('2024-06-01T00:00:00Z', 'hour')
    expect(result).toMatch(/\d{1,2}:\d{2}/)
  })

  it('handles different months for day granularity', () => {
    const jan = formatBucketTime('2024-01-05T12:00:00Z', 'day')
    const dec = formatBucketTime('2024-12-25T12:00:00Z', 'day')
    // Both should have month abbreviations
    expect(jan).toMatch(/[A-Z][a-z]{2}/)
    expect(dec).toMatch(/[A-Z][a-z]{2}/)
    // They should be different
    expect(jan).not.toBe(dec)
  })
})

describe('getTimeRangeLabel', () => {
  it('returns correct labels for all time ranges', () => {
    expect(getTimeRangeLabel('1h')).toBe('Last 1 hour')
    expect(getTimeRangeLabel('24h')).toBe('Last 24 hours')
    expect(getTimeRangeLabel('7d')).toBe('Last 7 days')
    expect(getTimeRangeLabel('30d')).toBe('Last 30 days')
  })
})

describe('getTimeRangeDates', () => {
  it('returns ISO date strings', () => {
    const result = getTimeRangeDates('24h')
    expect(result.startDate).toMatch(/\d{4}-\d{2}-\d{2}T/)
    expect(result.endDate).toMatch(/\d{4}-\d{2}-\d{2}T/)
  })

  it('returns start before end', () => {
    const result = getTimeRangeDates('7d')
    expect(new Date(result.startDate).getTime()).toBeLessThan(new Date(result.endDate).getTime())
  })
})
