import { InlineKeyboard } from 'grammy';

export function formatConfirmationMessage(tool: string, args: Record<string, unknown>): string {
  const argsStr = Object.entries(args)
    .map(([key, value]) => `  ${key}: ${JSON.stringify(value)}`)
    .join('\n');

  return `🔐 **Confirmation Required**

Tool: \`${tool}\`
Arguments:
${argsStr || '  (none)'}

Approve this action?`;
}

export function createConfirmationKeyboard(confirmationId: string): InlineKeyboard {
  return new InlineKeyboard()
    .text('✅ Approve', `confirm:${confirmationId}:approve`)
    .text('❌ Deny', `confirm:${confirmationId}:deny`);
}

export interface ConfirmationCallbackData {
  id: string;
  approved: boolean;
}

export function parseConfirmationCallback(data: string): ConfirmationCallbackData | null {
  const parts = data.split(':');
  if (parts.length !== 3 || parts[0] !== 'confirm') {
    return null;
  }

  const [, id, action] = parts;
  if (action !== 'approve' && action !== 'deny') {
    return null;
  }

  return {
    id,
    approved: action === 'approve',
  };
}
