import { describe, expect, it } from 'vitest'

import {
  inferColumnV0,
  inferLifecycle,
  type LifecycleEvent,
  workingDaysBetween,
} from './lifecycle-v0.js'

const base = { state: 'open' as const, labels: [] as string[], milestone: null }

describe('inferColumnV0', () => {
  it('closed issues are done', () => {
    expect(inferColumnV0({ ...base, state: 'closed' })).toBe('done')
  })

  it('open + labelled is triage', () => {
    expect(inferColumnV0({ ...base, labels: ['est/2d'] })).toBe('triage')
  })

  it('open + milestoned is triage', () => {
    expect(inferColumnV0({ ...base, milestone: { id: 6, title: 'P1', dueOn: null } })).toBe('triage')
  })

  it('open + bare is diagnosis', () => {
    expect(inferColumnV0(base)).toBe('diagnosis')
  })

  it('never guesses steeping or review in v0', () => {
    // even a closed, labelled, milestoned issue resolves to a real column, never the event-only ones
    const col = inferColumnV0({ ...base, labels: ['est/2d', 'p/1'], milestone: { id: 6, title: 'P1', dueOn: null } })
    expect(['steeping', 'review']).not.toContain(col)
  })
})

describe('workingDaysBetween', () => {
  it('counts weekdays in [start, end), excluding weekends', () => {
    // Mon 2026-01-05 → Mon 2026-01-12 spans a full week = 5 working days
    expect(workingDaysBetween(new Date('2026-01-05'), new Date('2026-01-12'))).toBe(5)
  })

  it('is 0 for same day or reversed', () => {
    expect(workingDaysBetween(new Date('2026-01-05'), new Date('2026-01-05'))).toBe(0)
    expect(workingDaysBetween(new Date('2026-01-12'), new Date('2026-01-05'))).toBe(0)
  })

  it('skips a weekend inside the interval', () => {
    // Fri 2026-01-09 → Mon 2026-01-12: only Fri counts (Sat/Sun excluded) = 1
    expect(workingDaysBetween(new Date('2026-01-09'), new Date('2026-01-12'))).toBe(1)
  })
})

describe('inferLifecycle', () => {
  const openBase = { ...base, createdAt: '2026-01-05T09:00:00Z', closedAt: null }
  const asOf = new Date('2026-01-14T09:00:00Z')

  const ev = (type: LifecycleEvent['type'], at: string): LifecycleEvent => ({ type, at })

  it('bare open issue with no events is diagnosis', () => {
    const inf = inferLifecycle(openBase, [], asOf)
    expect(inf.column).toBe('diagnosis')
    expect(inf.stages.opened).toBe(openBase.createdAt)
    expect(inf.actualWorkingDays).toBeNull()
    expect(inf.steepingDays).toBeNull()
  })

  it('a triage event (or a current label) moves it to triage', () => {
    expect(inferLifecycle(openBase, [ev('triage', '2026-01-06T09:00:00Z')], asOf).column).toBe('triage')
    expect(inferLifecycle({ ...openBase, labels: ['p/1'] }, [], asOf).column).toBe('triage')
  })

  it('a commit ref moves an open issue to steeping and counts its working age', () => {
    const inf = inferLifecycle(
      { ...openBase, labels: ['est/2d'] },
      [ev('triage', '2026-01-05T10:00:00Z'), ev('commit', '2026-01-07T12:00:00Z')],
      asOf,
    )
    expect(inf.column).toBe('steeping')
    expect(inf.stages.steeping).toBe('2026-01-07T12:00:00Z')
    // Wed 2026-01-07 → Wed 2026-01-14 = 5 working days
    expect(inf.steepingDays).toBe(5)
  })

  it('a pull ref outranks a commit ref → review', () => {
    const inf = inferLifecycle(
      openBase,
      [ev('commit', '2026-01-07T12:00:00Z'), ev('pull', '2026-01-08T12:00:00Z')],
      asOf,
    )
    expect(inf.column).toBe('review')
    expect(inf.steepingDays).toBeNull() // only steeping issues carry an age
  })

  it('closed → done, with actual working time from work-start to close', () => {
    const inf = inferLifecycle(
      { ...openBase, state: 'closed', closedAt: '2026-01-12T09:00:00Z' },
      [ev('commit', '2026-01-07T09:00:00Z'), ev('pull', '2026-01-08T09:00:00Z'), ev('close', '2026-01-12T09:00:00Z')],
      asOf,
    )
    expect(inf.column).toBe('done')
    expect(inf.stages.done).toBe('2026-01-12T09:00:00Z')
    // Wed 2026-01-07 → Mon 2026-01-12 = Wed,Thu,Fri = 3 working days
    expect(inf.actualWorkingDays).toBe(3)
  })

  it('takes the earliest event of each kind for stage timestamps', () => {
    const inf = inferLifecycle(
      openBase,
      [ev('commit', '2026-01-09T09:00:00Z'), ev('commit', '2026-01-07T09:00:00Z')],
      asOf,
    )
    expect(inf.stages.steeping).toBe('2026-01-07T09:00:00Z')
  })

  it('falls back to opened when a closed issue has no work-start signal', () => {
    const inf = inferLifecycle(
      { ...openBase, state: 'closed', closedAt: '2026-01-08T09:00:00Z' },
      [ev('close', '2026-01-08T09:00:00Z')],
      asOf,
    )
    // Mon 2026-01-05 → Thu 2026-01-08 = Mon,Tue,Wed = 3 working days
    expect(inf.actualWorkingDays).toBe(3)
  })
})
