import type { CommitMessageResult } from "@/commands/commit/types";

export type OutputFormat = 'text' | 'json' | 'github';

export class OutputFormatter {
  static formatCommitResult(
    result: CommitMessageResult, 
    format: OutputFormat = 'text'
  ): string {
    switch (format) {
      case 'json':
        return JSON.stringify(result, null, 2);
      
      case 'github':
        return this.formatAsGitHubComment(result);
      
      case 'text':
      default:
        return this.formatAsText(result);
    }
  }

  private static formatAsText(result: CommitMessageResult): string {
    const parts = [];
    
    if (result.emoji) parts.push(result.emoji);
    parts.push(`${result.type}${result.scope ? `(${result.scope})` : ''}: ${result.subject}`);
    
    let message = parts.join(' ');
    
    if (result.body) {
      message += `\n\n${result.body}`;
    }
    
    return message;
  }

  private static formatAsGitHubComment(result: CommitMessageResult): string {
    const emoji = result.emoji || '✨';
    const scope = result.scope ? ` (${result.scope})` : '';
    
    let comment = `## ${emoji} Suggested Commit Message\n\n`;
    comment += `\`\`\`\n`;
    comment += `${result.type}${scope}: ${result.subject}\n`;
    if (result.body) {
      comment += `\n${result.body}\n`;
    }
    comment += `\`\`\`\n\n`;
    comment += `**Type:** \`${result.type}\`\n`;
    if (result.scope) comment += `**Scope:** \`${result.scope}\`\n`;
    comment += `**Subject:** ${result.subject}\n`;
    
    return comment;
  }

  static formatError(error: string, format: OutputFormat = 'text'): string {
    switch (format) {
      case 'json':
        return JSON.stringify({ error }, null, 2);
      
      case 'github':
        return `## ❌ Error\n\n${error}`;
      
      case 'text':
      default:
        return error;
    }
  }
}