import { z } from 'zod'
import { pikkuSessionlessFunc } from '#pikku'

export const EnrichCardInput = z.object({
  title: z.string().min(1),
  status: z.enum(['todo', 'doing', 'done']).default('todo'),
})

export const EnrichCardOutput = z.object({
  title: z.string(),
  status: z.string(),
  priority: z.enum(['low', 'medium', 'high']),
  labels: z.array(z.string()),
})

export const enrichCard = pikkuSessionlessFunc({
  inline: false,
  input: EnrichCardInput,
  output: EnrichCardOutput,
  func: async ({ logger }, { title, status }) => {
    const lower = title.toLowerCase()
    let priority: 'low' | 'medium' | 'high' = 'medium'
    if (/\b(urgent|asap|critical|p0|outage)\b/.test(lower)) priority = 'high'
    else if (/\b(later|someday|maybe|nice to have)\b/.test(lower)) priority = 'low'
    const labels: string[] = []
    if (/\b(bug|fix|broken|error)\b/.test(lower)) labels.push('bug')
    if (/\b(feat|feature|add)\b/.test(lower)) labels.push('feature')
    logger.info(`enrichCard: '${title}' → ${priority}`)
    return { title, status, priority, labels }
  },
})
