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

vi.mock('@/lib/prisma', () => ({ prisma: {} }))

import { computeBurndownDays } from '@/lib/insights/burndown'

describe('computeBurndownDays', () => {
  it('5-day sprint: remaining and ideal match spec', () => {
    const start = new Date('2024-01-01T00:00:00.000Z')
    const end = new Date('2024-01-05T00:00:00.000Z')

    const tasks = [
      { status: 'DONE', updated_at: new Date('2024-01-02T12:00:00.000Z') },
      { status: 'DONE', updated_at: new Date('2024-01-04T12:00:00.000Z') },
      { status: 'IN_PROGRESS', updated_at: new Date('2024-01-05T12:00:00.000Z') },
    ]

    const days = computeBurndownDays(tasks, start, end)

    expect(days).toHaveLength(5)
    expect(days.map(d => d.remaining)).toEqual([3, 2, 2, 1, 1])
    expect(days.map(d => d.ideal)).toEqual([3, 2.25, 1.5, 0.75, 0])
    expect(days.map(d => d.day)).toEqual([
      '2024-01-01',
      '2024-01-02',
      '2024-01-03',
      '2024-01-04',
      '2024-01-05',
    ])
  })

  it('returns empty array when end is before start', () => {
    const start = new Date('2024-01-05T00:00:00.000Z')
    const end = new Date('2024-01-01T00:00:00.000Z')
    expect(computeBurndownDays([], start, end)).toEqual([])
  })

  it('single-day sprint has ideal = 0', () => {
    const day = new Date('2024-01-01T00:00:00.000Z')
    const tasks = [{ status: 'TO_DO', updated_at: new Date('2024-01-01T08:00:00.000Z') }]
    const days = computeBurndownDays(tasks, day, day)
    expect(days).toHaveLength(1)
    expect(days[0].ideal).toBe(0)
    expect(days[0].remaining).toBe(1)
  })

  it('all tasks done on first day: remaining drops to 0', () => {
    const start = new Date('2024-01-01T00:00:00.000Z')
    const end = new Date('2024-01-03T00:00:00.000Z')
    const tasks = [
      { status: 'DONE', updated_at: new Date('2024-01-01T10:00:00.000Z') },
      { status: 'DONE', updated_at: new Date('2024-01-01T11:00:00.000Z') },
    ]
    const days = computeBurndownDays(tasks, start, end)
    expect(days.map(d => d.remaining)).toEqual([0, 0, 0])
  })
})
