import { describe, expect, it } from 'vitest'
import {
  buildPlanUnitEditPath,
  buildPlanUnitNewPath,
  buildPlanningHubReturnState,
  isPlanningUnitEditorPath,
  legacyPlanningUnitDeepLinkTarget,
  parsePlanningHubQuery,
  planningHubPathFromReturnState,
} from './planningUnitRoutes.js'

describe('planningUnitRoutes', () => {
  it('buildPlanUnitEditPath', () => {
    expect(buildPlanUnitEditPath(42)).toBe('/planning/units/42/edit')
    expect(buildPlanUnitEditPath('7')).toBe('/planning/units/7/edit')
    expect(buildPlanUnitEditPath(0)).toBe('/planning')
  })

  it('buildPlanUnitNewPath with query', () => {
    expect(buildPlanUnitNewPath({ groupId: 3, plannedDate: '2026-05-20' })).toBe(
      '/planning/units/new?group=3&date=2026-05-20'
    )
    expect(buildPlanUnitNewPath()).toBe('/planning/units/new')
  })

  it('isPlanningUnitEditorPath', () => {
    expect(isPlanningUnitEditorPath('/planning/units/new')).toBe(true)
    expect(isPlanningUnitEditorPath('/planning/units/42/edit')).toBe(true)
    expect(isPlanningUnitEditorPath('/planning')).toBe(false)
    expect(isPlanningUnitEditorPath('/planning/training-modules')).toBe(false)
  })

  it('legacyPlanningUnitDeepLinkTarget', () => {
    expect(legacyPlanningUnitDeepLinkTarget('unit=5')).toBe('/planning/units/5/edit')
    expect(legacyPlanningUnitDeepLinkTarget('unit=5&debrief=1')).toBe(
      '/planning/units/5/edit?mode=debrief'
    )
    expect(legacyPlanningUnitDeepLinkTarget('')).toBeNull()
  })

  it('planning hub return roundtrip', () => {
    const state = buildPlanningHubReturnState({
      selectedGroupId: '12',
      planView: 'calendar',
      calendarMonthStr: '2026-06',
      assignedToMeOnly: true,
    })
    const path = planningHubPathFromReturnState(state)
    expect(path).toContain('/planning?')
    expect(path).toContain('group=12')
    expect(path).toContain('view=calendar')
    expect(path).toContain('month=2026-06')
    expect(path).toContain('mine=1')

    const parsed = parsePlanningHubQuery(path.split('?')[1] || '')
    expect(parsed.selectedGroupId).toBe('12')
    expect(parsed.planView).toBe('calendar')
    expect(parsed.calendarMonthStr).toBe('2026-06')
    expect(parsed.assignedToMeOnly).toBe(true)
  })
})
