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

export function makeDoneHandler(trello: TrelloClient) {
  return async function doneHandler(ctx: Context): Promise<void> {
    const result = await trello.getCardsByListName('done');
    if (!result) {
      await ctx.reply('No "Done" list found on the board. Check /list to see list names.');
      return;
    }
    const { list, cards } = result;
    const recent = cards.slice(0, 20);
    if (recent.length === 0) {
      await ctx.replyWithMarkdownV2(
        `${bold(escapeMarkdown(list.name))}\n\n_Nothing completed yet\\._`,
      );
      return;
    }
    const rows = recent.map((c) => cardLine(c.name, c.shortUrl)).join('\n');
    const suffix = cards.length > 20 ? `\n\n_Showing 20 of ${cards.length} completed cards_` : '';
    await ctx.replyWithMarkdownV2(
      `${bold(escapeMarkdown(list.name))} \\(${cards.length}\\)\n\n${rows}${suffix}`,
    );
  };
}
