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

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

const TASK_DB_TO_API = {
  TO_DO: 'todo',
  IN_PROGRESS: 'in_progress',
  REVIEW: 'review',
  DONE: 'done',
  FAILED: 'failed',
  EXCLUDED: 'excluded',
} 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',
  failed: 'FAILED',
  excluded: 'EXCLUDED',
}

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

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

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

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

const SPRINT_DB_TO_API = {
  OPEN: 'open',
  CLOSED: 'closed',
  ARCHIVED: 'archived',
  FAILED: 'failed',
} as const satisfies Record<SprintStatus, string>

const SPRINT_API_TO_DB: Record<string, SprintStatus> = {
  open: 'OPEN',
  closed: 'CLOSED',
  archived: 'ARCHIVED',
  failed: 'FAILED',
}

const SPRINT_RUN_DB_TO_API = {
  QUEUED: 'queued',
  RUNNING: 'running',
  PAUSED: 'paused',
  DONE: 'done',
  FAILED: 'failed',
  CANCELLED: 'cancelled',
} as const satisfies Record<SprintRunStatus, string>

const SPRINT_RUN_API_TO_DB: Record<string, SprintRunStatus> = {
  queued: 'QUEUED',
  running: 'RUNNING',
  paused: 'PAUSED',
  done: 'DONE',
  failed: 'FAILED',
  cancelled: 'CANCELLED',
}

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 type SprintStatusApi = typeof SPRINT_DB_TO_API[SprintStatus]
export type SprintRunStatusApi = typeof SPRINT_RUN_DB_TO_API[SprintRunStatus]

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 function sprintStatusToApi(s: SprintStatus): SprintStatusApi {
  return SPRINT_DB_TO_API[s]
}

export function sprintStatusFromApi(s: string): SprintStatus | null {
  return SPRINT_API_TO_DB[s.toLowerCase()] ?? null
}

export function sprintRunStatusToApi(s: SprintRunStatus): SprintRunStatusApi {
  return SPRINT_RUN_DB_TO_API[s]
}

export function sprintRunStatusFromApi(s: string): SprintRunStatus | null {
  return SPRINT_RUN_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)
export const SPRINT_STATUS_API_VALUES = Object.values(SPRINT_DB_TO_API)
export const SPRINT_RUN_STATUS_API_VALUES = Object.values(SPRINT_RUN_DB_TO_API)
