import { describe, it, expect } from 'vitest'

import {
  taskStatusToApi,
  taskStatusFromApi,
  storyStatusToApi,
  storyStatusFromApi,
  pbiStatusToApi,
  pbiStatusFromApi,
  TASK_STATUS_API_VALUES,
  STORY_STATUS_API_VALUES,
  PBI_STATUS_API_VALUES,
} from '@/lib/task-status'

describe('task-status mappers', () => {
  describe('taskStatus', () => {
    it('round-trips every API value', () => {
      for (const api of TASK_STATUS_API_VALUES) {
        const db = taskStatusFromApi(api)
        expect(db).not.toBeNull()
        expect(taskStatusToApi(db!)).toBe(api)
      }
    })

    it('returns null for invalid input', () => {
      expect(taskStatusFromApi('NOT_A_STATUS')).toBeNull()
    })

    it('is case-insensitive on the API side', () => {
      expect(taskStatusFromApi('IN_PROGRESS')).toBe('IN_PROGRESS')
      expect(taskStatusFromApi('In_Progress')).toBe('IN_PROGRESS')
    })
  })

  describe('storyStatus', () => {
    it('round-trips every API value', () => {
      for (const api of STORY_STATUS_API_VALUES) {
        const db = storyStatusFromApi(api)
        expect(db).not.toBeNull()
        expect(storyStatusToApi(db!)).toBe(api)
      }
    })

    it('returns null for invalid input', () => {
      expect(storyStatusFromApi('archived')).toBeNull()
    })
  })

  describe('pbiStatus', () => {
    it('round-trips every API value', () => {
      for (const api of PBI_STATUS_API_VALUES) {
        const db = pbiStatusFromApi(api)
        expect(db).not.toBeNull()
        expect(pbiStatusToApi(db!)).toBe(api)
      }
    })

    it('maps DB to lowercase API', () => {
      expect(pbiStatusToApi('READY')).toBe('ready')
      expect(pbiStatusToApi('BLOCKED')).toBe('blocked')
      expect(pbiStatusToApi('DONE')).toBe('done')
    })

    it('maps API to UPPER_SNAKE DB', () => {
      expect(pbiStatusFromApi('ready')).toBe('READY')
      expect(pbiStatusFromApi('blocked')).toBe('BLOCKED')
      expect(pbiStatusFromApi('done')).toBe('DONE')
    })

    it('is case-insensitive on the API side', () => {
      expect(pbiStatusFromApi('READY')).toBe('READY')
      expect(pbiStatusFromApi('Blocked')).toBe('BLOCKED')
    })

    it('returns null for invalid input', () => {
      expect(pbiStatusFromApi('archived')).toBeNull()
      expect(pbiStatusFromApi('')).toBeNull()
      expect(pbiStatusFromApi('todo')).toBeNull()
    })

    it('exposes alle vier API values', () => {
      expect(PBI_STATUS_API_VALUES).toEqual(['ready', 'blocked', 'failed', 'done'])
    })
  })
})
