import { wireCLI, pikkuCLICommand, pikkuCLIRender } from '#pikku'
import { listCards } from '../functions/list-cards.function.js'
import { createCard } from '../functions/create-card.function.js'

const cardsRenderer = pikkuCLIRender<{
  cards: Array<{ cardId: string; title: string; status: string; position: number }>
}>((_services, { cards }) => {
  if (cards.length === 0) {
    console.log('No cards on the board.')
    return
  }
  for (const card of cards) {
    console.log(`[${card.status.toUpperCase()}] ${card.position}. ${card.title} (${card.cardId})`)
  }
})

const cardRenderer = pikkuCLIRender<{ cardId: string; title: string; status: string }>(
  (_services, card) => {
    console.log(`Created: [${card.status}] ${card.title} (${card.cardId})`)
  },
)

wireCLI({
  program: 'kanban',
  commands: {
    list: pikkuCLICommand({
      func: listCards,
      description: 'List all kanban cards',
      render: cardsRenderer,
    }),
    add: pikkuCLICommand({
      parameters: '<title>',
      func: createCard,
      description: 'Add a new kanban card',
      render: cardRenderer,
      options: {
        status: {
          description: 'Card status (todo, doing, done)',
          short: 's',
          default: 'todo' as const,
        },
      },
    }),
  },
  render: pikkuCLIRender((_services, result) => {
    console.log(JSON.stringify(result, null, 2))
  }),
})
