export type IntakeStatus = 'draft' | 'backlog' | 'review' | 'promoted' | 'rejected' | 'archived'

export const REJECT_REASON_CODES = [
  'duplicate',
  'out_of_scope',
  'insufficient_data',
  'wrong_department',
  'not_feasible',
  'other',
] as const

export type RejectReasonCode = (typeof REJECT_REASON_CODES)[number]

const ALLOWED_TRANSITIONS: Record<IntakeStatus, IntakeStatus[]> = {
  draft:     [],
  backlog:   ['review', 'rejected', 'promoted'],
  review:    ['backlog', 'rejected', 'promoted'],
  promoted:  [],
  rejected:  ['backlog'],
  archived:  [],
}

export function isAllowedTransition(from: IntakeStatus, to: IntakeStatus): boolean {
  return ALLOWED_TRANSITIONS[from]?.includes(to) ?? false
}

export function validateRejectInput(
  reasonCode: string,
  freeText: string | null,
): { ok: true } | { ok: false; error: string } {
  if (!(REJECT_REASON_CODES as readonly string[]).includes(reasonCode)) {
    return { ok: false, error: 'invalid reason code' }
  }
  if (reasonCode === 'other' && (!freeText || freeText.trim().length < 3)) {
    return { ok: false, error: 'free text required for reason "other"' }
  }
  return { ok: true }
}
