/**
 * Markdown to Email HTML Parser
 * 
 * Supports:
 * - Standard Markdown (headings, bold, italic, lists, links, horizontal rules)
 * - Card blocks with ::: syntax
 * - Button blocks with [button url="..."]Text[/button] syntax
 * - Variables with {variable_name}
 * - Checkmarks (✓) and bullet points (•)
 */

export function parseMarkdownToEmail(markdown: string): string {
  let html = markdown;

  // Parse card blocks first (:::card or :::card[type])
  html = html.replace(/:::card(?:\[(\w+)\])?\n([\s\S]*?):::/g, (match, type, content) => {
    const cardType = type || 'default';
    const parsedContent = parseMarkdownBasics(content.trim());
    return `[card${type ? ` type="${cardType}"` : ''}]\n${parsedContent}\n[/card]`;
  });

  // Parse button blocks [button url="..."]Text[/button] - already in correct format
  // Also support legacy [button](url){text} syntax
  html = html.replace(/\[button(?:\s+style="(solid|outline)")?\]\((.*?)\)\s*\{([^}]+)\}/g, (match, style, url, text) => {
    return `[button url="${url}"${style ? ` style="${style}"` : ''}]${text}[/button]`;
  });
  
  // Horizontal rules
  html = html.replace(/^---$/gm, '<hr>');

  // Parse remaining markdown (outside cards)
  html = parseMarkdownBasics(html);

  return html;
}

function parseMarkdownBasics(text: string): string {
  let html = text;

  // Headings
  html = html.replace(/^#### (.*$)/gim, '<h4>$1</h4>');
  html = html.replace(/^### (.*$)/gim, '<h3>$1</h3>');
  html = html.replace(/^## (.*$)/gim, '<h2>$1</h2>');
  html = html.replace(/^# (.*$)/gim, '<h1>$1</h1>');

  // Bold
  html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
  html = html.replace(/__(.*?)__/g, '<strong>$1</strong>');

  // Italic
  html = html.replace(/\*(.*?)\*/g, '<em>$1</em>');
  html = html.replace(/_(.*?)_/g, '<em>$1</em>');

  // Links (but not button syntax)
  html = html.replace(/\[(?!button)([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');

  // Unordered lists (including checkmarks and bullets)
  html = html.replace(/^[\*\-•✓] (.*$)/gim, '<li>$1</li>');
  html = html.replace(/(<li>.*<\/li>)/s, '<ul>$1</ul>');

  // Ordered lists
  html = html.replace(/^\d+\. (.*$)/gim, '<li>$1</li>');
  
  // Paragraphs (lines not already in tags)
  const lines = html.split('\n');
  const processedLines = lines.map(line => {
    const trimmed = line.trim();
    if (!trimmed) return '';
    if (trimmed.startsWith('<') || trimmed.startsWith('[')) return line;
    return `<p>${line}</p>`;
  });
  html = processedLines.join('\n');

  return html;
}

/**
 * Convert email HTML back to Markdown
 */
export function parseEmailToMarkdown(html: string): string {
  let markdown = html;

  // Convert [card] blocks to ::: syntax
  markdown = markdown.replace(/\[card(?:\s+type="(\w+)")?\]([\s\S]*?)\[\/card\]/g, (match, type, content) => {
    const mdContent = parseHtmlToMarkdownBasics(content.trim());
    return type ? `:::card[${type}]\n${mdContent}\n:::` : `:::card\n${mdContent}\n:::`;
  });

  // Convert [button] blocks - keep new syntax [button url="..."]Text[/button]
  // This is already the format we want, so just normalize
  markdown = markdown.replace(/\[button link="([^"]+)"(?:\s+style="(solid|outline)")?\]([^[]+)\[\/button\]/g, (match, url, style, text) => {
    return `[button url="${url}"${style ? ` style="${style}"` : ''}]${text.trim()}[/button]`;
  });
  
  // Convert horizontal rules
  markdown = markdown.replace(/<hr\s*\/?>/gi, '\n---\n');

  // Convert remaining HTML to markdown
  markdown = parseHtmlToMarkdownBasics(markdown);

  return markdown;
}

function parseHtmlToMarkdownBasics(html: string): string {
  let markdown = html;

  // Headings
  markdown = markdown.replace(/<h1>(.*?)<\/h1>/gi, '# $1');
  markdown = markdown.replace(/<h2>(.*?)<\/h2>/gi, '## $1');
  markdown = markdown.replace(/<h3>(.*?)<\/h3>/gi, '### $1');
  markdown = markdown.replace(/<h4>(.*?)<\/h4>/gi, '#### $1');

  // Bold
  markdown = markdown.replace(/<strong>(.*?)<\/strong>/gi, '**$1**');
  markdown = markdown.replace(/<b>(.*?)<\/b>/gi, '**$1**');

  // Italic
  markdown = markdown.replace(/<em>(.*?)<\/em>/gi, '*$1*');
  markdown = markdown.replace(/<i>(.*?)<\/i>/gi, '*$1*');

  // Links
  markdown = markdown.replace(/<a href="([^"]+)">(.*?)<\/a>/gi, '[$2]($1)');

  // Lists
  markdown = markdown.replace(/<ul>([\s\S]*?)<\/ul>/gi, (match, content) => {
    return content.replace(/<li>(.*?)<\/li>/gi, '- $1\n');
  });
  markdown = markdown.replace(/<ol>([\s\S]*?)<\/ol>/gi, (match, content) => {
    let counter = 1;
    return content.replace(/<li>(.*?)<\/li>/gi, () => `${counter++}. $1\n`);
  });

  // Paragraphs
  markdown = markdown.replace(/<p>(.*?)<\/p>/gi, '$1\n\n');

  // Clean up extra newlines
  markdown = markdown.replace(/\n{3,}/g, '\n\n');

  return markdown.trim();
}
