import { describe, it, expect } from 'vitest'
import {
  deriveScreenState,
  type ScreenStateInput,
} from '@/stores/product-workspace/screen-state'

const base: ScreenStateInput = {
  activeSprintItem: null,
  buildingSprintIds: [],
  hasPendingDraft: false,
  pendingAdds: [],
  pendingRemoves: [],
}

describe('deriveScreenState', () => {
  it('returns NO_SPRINT without draft or active sprint', () => {
    expect(deriveScreenState(base)).toEqual({ kind: 'NO_SPRINT' })
  })

  it('returns DRAFT when a pending draft exists', () => {
    expect(deriveScreenState({ ...base, hasPendingDraft: true })).toEqual({
      kind: 'DRAFT',
    })
  })

  it('lets a draft win over an active sprint with pending changes', () => {
    expect(
      deriveScreenState({
        ...base,
        hasPendingDraft: true,
        activeSprintItem: { id: 's1' },
        pendingAdds: ['x'],
      }),
    ).toEqual({ kind: 'DRAFT' })
  })

  it('returns ACTIVE for an active sprint with no pending changes', () => {
    expect(
      deriveScreenState({ ...base, activeSprintItem: { id: 's1' } }),
    ).toEqual({ kind: 'ACTIVE', building: false })
  })

  it('flags building when the active sprint is in buildingSprintIds', () => {
    expect(
      deriveScreenState({
        ...base,
        activeSprintItem: { id: 's1' },
        buildingSprintIds: ['s1'],
      }),
    ).toEqual({ kind: 'ACTIVE', building: true })
  })

  it('returns EDITING when there are pending adds', () => {
    expect(
      deriveScreenState({
        ...base,
        activeSprintItem: { id: 's1' },
        pendingAdds: ['x'],
      }),
    ).toEqual({ kind: 'EDITING', building: false })
  })

  it('returns EDITING when there are pending removes', () => {
    expect(
      deriveScreenState({
        ...base,
        activeSprintItem: { id: 's1' },
        pendingRemoves: ['y'],
      }),
    ).toEqual({ kind: 'EDITING', building: false })
  })

  it('flags building on EDITING when the active sprint is building', () => {
    expect(
      deriveScreenState({
        ...base,
        activeSprintItem: { id: 's1' },
        pendingAdds: ['x'],
        buildingSprintIds: ['s1'],
      }),
    ).toEqual({ kind: 'EDITING', building: true })
  })
})
