import { describe, it, expect } from 'vitest'
import {
  classifyRecordStatus,
  classifySchemaStatus,
} from './MyDraftsPanel'
import type { DraftStatus, SchemaDraftStatus } from '@/api/client'

describe('classifyRecordStatus', () => {
  it('PENDING → pending bucket', () => {
    expect(classifyRecordStatus('PENDING')).toBe('pending')
  })

  it.each<DraftStatus>(['APPROVED', 'REJECTED', 'WITHDRAWN'])(
    'terminal status %s → decided bucket',
    (s) => {
      expect(classifyRecordStatus(s)).toBe('decided')
    },
  )

  it('covers all DraftStatus variants — exhaustiveness check', () => {
    // Sanity: every DraftStatus literal must land in exactly one bucket.
    // If this fails after adding a new status, update the classifier and
    // the bucket count tests in ReviewsPage's myActiveCount memo.
    const all: DraftStatus[] = ['PENDING', 'APPROVED', 'REJECTED', 'WITHDRAWN']
    const buckets = all.map(classifyRecordStatus)
    expect(new Set(buckets)).toEqual(new Set(['pending', 'decided']))
  })
})

describe('classifySchemaStatus', () => {
  it('review_pending → pending (reviewer action needed)', () => {
    expect(classifySchemaStatus('review_pending')).toBe('pending')
  })

  it('approved → pending (maker still needs to publish)', () => {
    expect(classifySchemaStatus('approved')).toBe('pending')
  })

  it('draft → wip (maker action needed)', () => {
    expect(classifySchemaStatus('draft')).toBe('wip')
  })

  it('changes_requested → wip (reviewer returned to maker)', () => {
    expect(classifySchemaStatus('changes_requested')).toBe('wip')
  })

  it.each<SchemaDraftStatus>(['rejected', 'published', 'withdrawn'])(
    'terminal status %s → decided',
    (s) => {
      expect(classifySchemaStatus(s)).toBe('decided')
    },
  )

  it('covers all SchemaDraftStatus variants — exhaustiveness check', () => {
    const all: SchemaDraftStatus[] = [
      'draft',
      'review_pending',
      'approved',
      'changes_requested',
      'rejected',
      'published',
      'withdrawn',
    ]
    const buckets = all.map(classifySchemaStatus)
    expect(new Set(buckets)).toEqual(new Set(['pending', 'wip', 'decided']))
  })
})
