// Bidirectionele case-mappers voor de REST API-boundary.
// DB houdt UPPER_SNAKE; API exposeert lowercase.

import type { TaskStatus, StoryStatus, PbiStatus } from '@prisma/client'

const TASK_DB_TO_API = {
  TO_DO: 'todo',
  IN_PROGRESS: 'in_progress',
  REVIEW: 'review',
  DONE: 'done',
} as const satisfies Record<TaskStatus, string>

const TASK_API_TO_DB: Record<string, TaskStatus> = {
  todo: 'TO_DO',
  in_progress: 'IN_PROGRESS',
  review: 'REVIEW',
  done: 'DONE',
}

const STORY_DB_TO_API = {
  OPEN: 'open',
  IN_SPRINT: 'in_sprint',
  DONE: 'done',
} as const satisfies Record<StoryStatus, string>

const STORY_API_TO_DB: Record<string, StoryStatus> = {
  open: 'OPEN',
  in_sprint: 'IN_SPRINT',
  done: 'DONE',
}

const PBI_DB_TO_API = {
  READY: 'ready',
  BLOCKED: 'blocked',
  DONE: 'done',
} as const satisfies Record<PbiStatus, string>

const PBI_API_TO_DB: Record<string, PbiStatus> = {
  ready: 'READY',
  blocked: 'BLOCKED',
  done: 'DONE',
}

export type TaskStatusApi = typeof TASK_DB_TO_API[TaskStatus]
export type StoryStatusApi = typeof STORY_DB_TO_API[StoryStatus]
export type PbiStatusApi = typeof PBI_DB_TO_API[PbiStatus]

export function taskStatusToApi(s: TaskStatus): TaskStatusApi {
  return TASK_DB_TO_API[s]
}

export function taskStatusFromApi(s: string): TaskStatus | null {
  return TASK_API_TO_DB[s.toLowerCase()] ?? null
}

export function storyStatusToApi(s: StoryStatus): StoryStatusApi {
  return STORY_DB_TO_API[s]
}

export function storyStatusFromApi(s: string): StoryStatus | null {
  return STORY_API_TO_DB[s.toLowerCase()] ?? null
}

export function pbiStatusToApi(s: PbiStatus): PbiStatusApi {
  return PBI_DB_TO_API[s]
}

export function pbiStatusFromApi(s: string): PbiStatus | null {
  return PBI_API_TO_DB[s.toLowerCase()] ?? null
}

export const TASK_STATUS_API_VALUES = Object.values(TASK_DB_TO_API)
export const STORY_STATUS_API_VALUES = Object.values(STORY_DB_TO_API)
export const PBI_STATUS_API_VALUES = Object.values(PBI_DB_TO_API)
