import type { Context } from 'telegraf';
import type { TrelloClient } from '../trello/client.js';
import { generateTextCLI } from '../gemini/cli.js';
import { sendChunked } from '../agent/chunker.js';
import { env } from '../env.js';

export function makeStatusHandler(trello: TrelloClient, googleApiKey: string) {
  return async function statusHandler(ctx: Context): Promise<void> {
    await ctx.reply('📊 Fetching board status...');

    const summary = await trello.getBoardSummary();
    const totalCards = summary.reduce((acc, e) => acc + e.count, 0);

    const boardText = summary
      .map((e) => `- ${e.list.name}: ${e.count} card${e.count !== 1 ? 's' : ''}`)
      .join('\n');

    const prompt = `${env.CUSTOM_SYSTEM_PROMPT}

You are a project manager assistant for Hetzi, a Hebrew-first Israeli deals marketplace (PWA, Astro + Cloudflare Workers stack).

Here is the current Trello board state:
${boardText}
Total open cards: ${totalCards}

Give a brief, practical status summary in 3-5 sentences. Highlight what's in progress, flag if anything looks stuck or overloaded, and end with one actionable suggestion. Be concise and direct.`;

    const text = await generateTextCLI(prompt, googleApiKey, 'status');
    await sendChunked(ctx, text);
  };
}
