package engine

import (
	"bytes"
	"log"
	"strings"

	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/parser"
	"github.com/yuin/goldmark/renderer/html"
	"github.com/yuin/goldmark/util"
)

// MarkdownProcessor handles minimal markdown processing
// Supports only: **bold**, *italic*, and [link](url)
type MarkdownProcessor struct {
	parser goldmark.Markdown
}

// NewMarkdownProcessor creates a new markdown processor with minimal configuration
func NewMarkdownProcessor() *MarkdownProcessor {
	// Configure goldmark to only support basic inline formatting
	md := goldmark.New(
		goldmark.WithParserOptions(
			parser.WithInlineParsers(
				// Bold (**text**) and italic (*text*) - same parser handles both
				util.Prioritized(parser.NewEmphasisParser(), 500),

				// Links [text](url)
				util.Prioritized(parser.NewLinkParser(), 600),
			),
			// Disable all block parsers except paragraph (no headings, lists, etc.)
			parser.WithBlockParsers(
				util.Prioritized(parser.NewParagraphParser(), 200),
			),
		),
		goldmark.WithRendererOptions(
			html.WithXHTML(),     // <br /> instead of <br>
			html.WithHardWraps(), // Line breaks become <br />
			html.WithUnsafe(),    // Allow existing HTML to pass through
		),
	)

	return &MarkdownProcessor{parser: md}
}

// ToHTML converts markdown string to HTML
func (mp *MarkdownProcessor) ToHTML(markdown string) (string, error) {
	if markdown == "" {
		return "", nil
	}

	var buf bytes.Buffer
	if err := mp.parser.Convert([]byte(markdown), &buf); err != nil {
		log.Printf("Markdown conversion failed: %v", err)
		return "", err
	}

	html := buf.String()

	// Clean up goldmark's paragraph wrapping for inline content
	// If content is wrapped in a single <p> tag, extract just the inner content
	html = strings.TrimSpace(html)

	if strings.HasPrefix(html, "<p>") && strings.HasSuffix(html, "</p>") {
		// Check if this is a single paragraph (no other <p> tags inside)
		inner := html[3 : len(html)-4] // Remove <p> and </p>
		if !strings.Contains(inner, "<p>") {
			// Single paragraph - return just the inner content for inline injection
			return inner, nil
		}
	}

	// Multiple paragraphs or other block content - return as-is
	return html, nil
}
