import { describe, it, expect } from 'bun:test'
import { isMinor, capBillingDay, todayISO } from '../../src/utils/dates.js'

describe('isMinor', () => {
  it('returns true for a child born 10 years ago', () => {
    const dob = new Date()
    dob.setFullYear(dob.getFullYear() - 10)
    expect(isMinor(dob)).toBe(true)
  })

  it('returns false for an adult born 25 years ago', () => {
    const dob = new Date()
    dob.setFullYear(dob.getFullYear() - 25)
    expect(isMinor(dob)).toBe(false)
  })

  it('returns true for someone turning 18 later this year', () => {
    const dob = new Date()
    dob.setFullYear(dob.getFullYear() - 18)
    dob.setMonth(dob.getMonth() + 1) // birthday is next month
    expect(isMinor(dob)).toBe(true)
  })

  it('returns false for someone who turned 18 earlier this year', () => {
    const dob = new Date()
    dob.setFullYear(dob.getFullYear() - 18)
    dob.setMonth(dob.getMonth() - 1) // birthday was last month
    expect(isMinor(dob)).toBe(false)
  })

  it('returns false on their 18th birthday', () => {
    const dob = new Date()
    dob.setFullYear(dob.getFullYear() - 18)
    expect(isMinor(dob)).toBe(false)
  })

  it('accepts a string date', () => {
    expect(isMinor('2000-01-01')).toBe(false)
    expect(isMinor('2020-01-01')).toBe(true)
  })

  it('returns true for a newborn', () => {
    expect(isMinor(new Date())).toBe(true)
  })
})

describe('capBillingDay', () => {
  it('caps at 28', () => {
    expect(capBillingDay(31)).toBe(28)
    expect(capBillingDay(29)).toBe(28)
  })

  it('floors at 1', () => {
    expect(capBillingDay(0)).toBe(1)
    expect(capBillingDay(-5)).toBe(1)
  })

  it('passes through valid days', () => {
    expect(capBillingDay(15)).toBe(15)
    expect(capBillingDay(1)).toBe(1)
    expect(capBillingDay(28)).toBe(28)
  })

  it('floors fractional days', () => {
    expect(capBillingDay(15.7)).toBe(15)
  })
})

describe('todayISO', () => {
  it('returns YYYY-MM-DD format', () => {
    const result = todayISO()
    expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/)
  })

  it('matches today', () => {
    const expected = new Date().toISOString().split('T')[0]
    expect(todayISO()).toBe(expected)
  })
})
