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

export const ListCardsInput = z.object({
  status: z.enum(['todo', 'doing', 'done']).optional(),
})
export const ListCardsOutput = z.object({
  cards: z.array(
    z.object({
      cardId: z.string(),
      title: z.string(),
      status: z.string(),
      position: z.number(),
      createdAt: z.string(),
    }),
  ),
})

export const listCards = pikkuSessionlessFunc({
  expose: true,
  description: 'List all kanban cards, optionally filtered by status.',
  input: ListCardsInput,
  output: ListCardsOutput,
  func: async ({ kysely, logger }, input) => {
    const status = input?.status
    let query = kysely.selectFrom('kanbanCard').selectAll().orderBy('position', 'asc')
    if (status) query = query.where('status', '=', status)
    const cards = await query.execute()
    logger.info(`listCards: ${cards.length} cards`)
    return { cards }
  },
})
