import type { Id } from "./_generated/dataModel"

export type ChecklistItemType = "checkbox" | "question"

export type TicketChecklistItem = {
  id: string
  text: string
  description?: string
  type?: ChecklistItemType
  options?: string[] // Para tipo "question": ["Sim", "Nao", ...]
  answer?: string // Resposta selecionada para tipo "question"
  done: boolean
  required?: boolean
  templateId?: Id<"ticketChecklistTemplates">
  templateItemId?: string
  templateDescription?: string // Descricao do template (copiada ao aplicar)
  createdAt?: number
  createdBy?: Id<"users">
  doneAt?: number
  doneBy?: Id<"users">
}

export type TicketChecklistTemplateItem = {
  id: string
  text: string
  description?: string
  type?: string // "checkbox" | "question" - string para compatibilidade com schema
  options?: string[]
  required?: boolean
}

export type TicketChecklistTemplateLike = {
  _id: Id<"ticketChecklistTemplates">
  description?: string
  items: TicketChecklistTemplateItem[]
}

export function normalizeChecklistText(input: string) {
  return input.replace(/\r\n/g, "\n").trim()
}

export function checklistBlocksResolution(checklist: TicketChecklistItem[] | null | undefined) {
  return (checklist ?? []).some((item) => (item.required ?? true) && item.done !== true)
}

export function applyChecklistTemplateToItems(
  existing: TicketChecklistItem[],
  template: TicketChecklistTemplateLike,
  options: {
    now: number
    actorId?: Id<"users">
    generateId?: () => string
  }
) {
  const generateId = options.generateId ?? (() => crypto.randomUUID())
  const now = options.now

  const next = Array.isArray(existing) ? [...existing] : []
  const existingKeys = new Set<string>()
  for (const item of next) {
    if (!item.templateId || !item.templateItemId) continue
    existingKeys.add(`${String(item.templateId)}:${item.templateItemId}`)
  }

  let added = 0
  for (const tplItem of template.items ?? []) {
    const templateItemId = String(tplItem.id ?? "").trim()
    const text = normalizeChecklistText(String(tplItem.text ?? ""))
    if (!templateItemId || !text) continue
    const key = `${String(template._id)}:${templateItemId}`
    if (existingKeys.has(key)) continue
    existingKeys.add(key)
    const itemType = tplItem.type ?? "checkbox"
    next.push({
      id: generateId(),
      text,
      description: tplItem.description,
      type: itemType as ChecklistItemType,
      options: itemType === "question" ? tplItem.options : undefined,
      done: false,
      required: typeof tplItem.required === "boolean" ? tplItem.required : true,
      templateId: template._id,
      templateItemId,
      templateDescription: template.description,
      createdAt: now,
      createdBy: options.actorId,
    })
    added += 1
  }

  return { checklist: next, added }
}

