import type { Context } from 'telegraf';
import type { TrelloClient } from '../trello/client.js';
import { bold, cardLine, escapeMarkdown } from '../utils/format.js';

export function makeTodoHandler(trello: TrelloClient) {
  return async function todoHandler(ctx: Context): Promise<void> {
    const result = await trello.getCardsByListName('todo');
    if (!result) {
      await ctx.reply('No "To Do" list found on the board. Check /list to see list names.');
      return;
    }
    const { list, cards } = result;
    if (cards.length === 0) {
      await ctx.replyWithMarkdownV2(`${bold(escapeMarkdown(list.name))}\n\n_Nothing to do\\! 🎉_`);
      return;
    }
    const rows = cards.map((c) => cardLine(c.name, c.shortUrl)).join('\n');
    await ctx.replyWithMarkdownV2(
      `${bold(escapeMarkdown(list.name))} \\(${cards.length}\\)\n\n${rows}`,
    );
  };
}
