import { describe, it, expect } from 'vitest'
import {
  consultantLabel,
  kwToDateRange,
  getWeekdaysInRange,
} from '@/components/project/project-edit-form.helpers'

describe('consultantLabel', () => {
  it('prefers display_name when present', () => {
    expect(consultantLabel({ display_name: 'Dr. Müller', first_name: 'Anna', last_name: 'Müller' })).toBe('Dr. Müller')
  })

  it('falls back to "Last, First" when display_name missing', () => {
    expect(consultantLabel({ display_name: null, first_name: 'Anna', last_name: 'Müller' })).toBe('Müller, Anna')
  })

  it('handles missing first name', () => {
    expect(consultantLabel({ display_name: null, first_name: null, last_name: 'Müller' })).toBe('Müller')
  })

  it('returns empty string when all fields missing', () => {
    expect(consultantLabel({ display_name: null, first_name: null, last_name: null })).toBe('')
  })
})

describe('kwToDateRange', () => {
  it('returns null for out-of-range kw', () => {
    expect(kwToDateRange(0, 2025)).toBeNull()
    expect(kwToDateRange(54, 2025)).toBeNull()
  })

  it('returns null for implausible year', () => {
    expect(kwToDateRange(10, 1999)).toBeNull()
  })

  it('computes ISO week 1 correctly (2025-01-01 is a Wed → week 1 starts 2024-12-30)', () => {
    expect(kwToDateRange(1, 2025)).toEqual({ start: '2024-12-30', end: '2025-01-03' })
  })

  it('computes week 23 of 2025 (a straightforward mid-year week)', () => {
    expect(kwToDateRange(23, 2025)).toEqual({ start: '2025-06-02', end: '2025-06-06' })
  })

  it('handles year with ISO week 53 (2020 has W53)', () => {
    expect(kwToDateRange(53, 2020)).toEqual({ start: '2020-12-28', end: '2021-01-01' })
  })

  it('always returns Monday→Friday (5 weekdays apart)', () => {
    const r = kwToDateRange(10, 2025)!
    const diff = (new Date(r.end).getTime() - new Date(r.start).getTime()) / 86400000
    expect(diff).toBe(4)
  })
})

describe('getWeekdaysInRange', () => {
  it('returns empty for missing input', () => {
    expect(getWeekdaysInRange('', '2025-01-10')).toEqual([])
    expect(getWeekdaysInRange('2025-01-10', '')).toEqual([])
  })

  it('returns empty when start > end', () => {
    expect(getWeekdaysInRange('2025-01-10', '2025-01-01')).toEqual([])
  })

  it('returns the single date if it is a weekday', () => {
    // 2025-06-04 is a Wednesday
    expect(getWeekdaysInRange('2025-06-04', '2025-06-04')).toEqual(['2025-06-04'])
  })

  it('returns empty if the single date is a weekend', () => {
    // 2025-06-07 is a Saturday
    expect(getWeekdaysInRange('2025-06-07', '2025-06-07')).toEqual([])
  })

  it('strips weekends from a Mon-Sun week', () => {
    // 2025-06-02 Mon … 2025-06-08 Sun
    expect(getWeekdaysInRange('2025-06-02', '2025-06-08')).toEqual([
      '2025-06-02',
      '2025-06-03',
      '2025-06-04',
      '2025-06-05',
      '2025-06-06',
    ])
  })

  it('spans multiple weeks correctly', () => {
    const days = getWeekdaysInRange('2025-06-02', '2025-06-13')
    expect(days).toHaveLength(10)
    expect(days[0]).toBe('2025-06-02')
    expect(days[days.length - 1]).toBe('2025-06-13')
  })
})
