import { describe, it, expect } from 'vitest'
import {
  validateHeaderInput,
  validateStationInput,
  nextStepFromCurrent,
  prevStepFromCurrent,
  STEP_KEYS,
  type HeaderInput,
  type StationInput,
} from '@/lib/oee/analysis'

const BASE_HEADER: HeaderInput = {
  name: 'Q2 Audit',
  project_id: 'p1',
  supplier_id: null,
  analysis_date: '2026-06-01',
  period_start: null,
  period_end: null,
}

describe('oee-analysis — header validation', () => {
  it('accepts a complete project-linked header', () => {
    expect(validateHeaderInput(BASE_HEADER, 'with_project').ok).toBe(true)
  })

  it('accepts an orphan header with supplier_id', () => {
    const r = validateHeaderInput({
      ...BASE_HEADER,
      project_id: null,
      supplier_id: 'sup1',
    })
    expect(r.ok).toBe(true)
  })

  it('rejects empty name', () => {
    const r = validateHeaderInput({ ...BASE_HEADER, name: '  ' })
    expect(r.ok).toBe(false)
    if (!r.ok) expect(r.error).toBe('name_required')
  })

  it('rejects missing analysis_date', () => {
    const r = validateHeaderInput({ ...BASE_HEADER, analysis_date: '' })
    expect(r.ok).toBe(false)
    if (!r.ok) expect(r.error).toBe('analysis_date_required')
  })

  it('rejects orphan without supplier_id', () => {
    const r = validateHeaderInput({
      ...BASE_HEADER,
      project_id: null,
      supplier_id: null,
    })
    expect(r.ok).toBe(false)
    if (!r.ok) expect(r.error).toBe('supplier_required_for_orphan')
  })

  it('rejects period_end before period_start', () => {
    const r = validateHeaderInput({
      ...BASE_HEADER,
      period_start: '2026-06-10',
      period_end: '2026-06-05',
    })
    expect(r.ok).toBe(false)
    if (!r.ok) expect(r.error).toBe('period_end_before_start')
  })

  it('accepts period without end', () => {
    expect(
      validateHeaderInput(
        { ...BASE_HEADER, period_start: '2026-06-01', period_end: null },
        'with_project',
      ).ok,
    ).toBe(true)
  })

  it('rejects with_project mode without project_id', () => {
    const r = validateHeaderInput(
      { ...BASE_HEADER, project_id: null, supplier_id: null },
      'with_project',
    )
    expect(r.ok).toBe(false)
    if (!r.ok) expect(r.error).toBe('project_required')
  })
})

describe('oee-analysis — station validation', () => {
  const BASE: StationInput = {
    name: 'Linie 1',
    linked_process_step_id: null,
    order_idx: 0,
  }

  it('accepts a complete station', () => {
    expect(validateStationInput(BASE).ok).toBe(true)
  })

  it('rejects empty name', () => {
    const r = validateStationInput({ ...BASE, name: '   ' })
    expect(r.ok).toBe(false)
    if (!r.ok) expect(r.error).toBe('name_required')
  })

  it('rejects negative or non-integer order_idx', () => {
    expect(validateStationInput({ ...BASE, order_idx: -1 }).ok).toBe(false)
    expect(validateStationInput({ ...BASE, order_idx: 1.5 }).ok).toBe(false)
  })
})

describe('oee-analysis — step navigation', () => {
  it('produces correct next-step', () => {
    expect(nextStepFromCurrent('grunddaten')).toBe('stationen')
    expect(nextStepFromCurrent('stationen')).toBe('messungen')
    expect(nextStepFromCurrent('messungen')).toBe('auswertung')
    expect(nextStepFromCurrent('auswertung')).toBeNull()
  })

  it('produces correct prev-step', () => {
    expect(prevStepFromCurrent('grunddaten')).toBeNull()
    expect(prevStepFromCurrent('stationen')).toBe('grunddaten')
    expect(prevStepFromCurrent('messungen')).toBe('stationen')
    expect(prevStepFromCurrent('auswertung')).toBe('messungen')
  })

  it('STEP_KEYS has exactly 4 in canonical order', () => {
    expect(STEP_KEYS).toEqual(['grunddaten', 'stationen', 'messungen', 'auswertung'])
  })
})
