import { z } from 'zod'

export const PauseReasonSchema = z.enum(['MERGE_CONFLICT'])
export type PauseReason = z.infer<typeof PauseReasonSchema>

export const PauseContextSchema = z.object({
  pause_reason: PauseReasonSchema,
  pr_url: z.string().url(),
  pr_head_sha: z.string().min(7),
  conflict_files: z.array(z.string()).default([]),
  claude_question_id: z.string().min(1),
  resume_instructions: z.string().min(1),
  paused_at: z.string().datetime(),
})

export type PauseContext = z.infer<typeof PauseContextSchema>

export function parsePauseContext(raw: unknown): PauseContext | null {
  if (raw === null || raw === undefined) return null
  const parsed = PauseContextSchema.safeParse(raw)
  return parsed.success ? parsed.data : null
}

const PAUSE_REASON_LABELS: Record<PauseReason, string> = {
  MERGE_CONFLICT: 'Merge-conflict op PR',
}

export function pauseReasonLabel(reason: PauseReason): string {
  return PAUSE_REASON_LABELS[reason] ?? reason
}
