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

export function makeDoingHandler(trello: TrelloClient) {
  return async function doingHandler(ctx: Context): Promise<void> {
    const result = await trello.getCardsByListName('doing');
    if (!result) {
      await ctx.reply('No "In Progress" 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 in progress\\._`,
      );
      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}`,
    );
  };
}
