package main

import (
	"fmt"
	"strings"

	"fn-registry/registry"
)

// renderFunctionMarkdown returns a markdown card for a Function:
// frontmatter-ish header + description + signature + code block.
func renderFunctionMarkdown(f *registry.Function) string {
	var b strings.Builder
	fmt.Fprintf(&b, "# %s\n\n", f.ID)
	fmt.Fprintf(&b, "- name: %s\n", f.Name)
	fmt.Fprintf(&b, "- kind: %s\n", f.Kind)
	fmt.Fprintf(&b, "- lang: %s\n", f.Lang)
	fmt.Fprintf(&b, "- domain: %s\n", f.Domain)
	fmt.Fprintf(&b, "- purity: %s\n", f.Purity)
	if f.Version != "" {
		fmt.Fprintf(&b, "- version: %s\n", f.Version)
	}
	if f.Signature != "" {
		fmt.Fprintf(&b, "- signature: `%s`\n", f.Signature)
	}
	if len(f.Returns) > 0 {
		fmt.Fprintf(&b, "- returns: %s\n", strings.Join(f.Returns, ", "))
	}
	if f.ErrorType != "" {
		fmt.Fprintf(&b, "- error_type: %s\n", f.ErrorType)
	}
	if len(f.UsesFunctions) > 0 {
		fmt.Fprintf(&b, "- uses_functions: %s\n", strings.Join(f.UsesFunctions, ", "))
	}
	if len(f.UsesTypes) > 0 {
		fmt.Fprintf(&b, "- uses_types: %s\n", strings.Join(f.UsesTypes, ", "))
	}
	if len(f.Tags) > 0 {
		fmt.Fprintf(&b, "- tags: %s\n", strings.Join(f.Tags, ", "))
	}
	if f.FilePath != "" {
		fmt.Fprintf(&b, "- file_path: %s\n", f.FilePath)
	}
	b.WriteString("\n")

	if f.Description != "" {
		b.WriteString(f.Description)
		b.WriteString("\n\n")
	}
	if f.ParamsSchema != "" {
		b.WriteString("## params\n\n```json\n")
		b.WriteString(f.ParamsSchema)
		b.WriteString("\n```\n\n")
	}
	if f.Documentation != "" {
		b.WriteString(f.Documentation)
		b.WriteString("\n\n")
	}
	if f.Code != "" {
		fmt.Fprintf(&b, "## code\n\n```%s\n%s\n```\n", langFence(f.Lang), f.Code)
	}
	if f.Example != "" {
		fmt.Fprintf(&b, "\n## example\n\n```%s\n%s\n```\n", langFence(f.Lang), f.Example)
	}
	if f.Notes != "" {
		fmt.Fprintf(&b, "\n## notes\n\n%s\n", f.Notes)
	}
	return b.String()
}

// renderTypeMarkdown returns a markdown card for a Type.
func renderTypeMarkdown(t *registry.Type) string {
	var b strings.Builder
	fmt.Fprintf(&b, "# %s (type)\n\n", t.ID)
	fmt.Fprintf(&b, "- name: %s\n", t.Name)
	fmt.Fprintf(&b, "- lang: %s\n", t.Lang)
	fmt.Fprintf(&b, "- domain: %s\n", t.Domain)
	fmt.Fprintf(&b, "- algebraic: %s\n", t.Algebraic)
	if t.Definition != "" {
		fmt.Fprintf(&b, "- definition: `%s`\n", t.Definition)
	}
	if len(t.UsesTypes) > 0 {
		fmt.Fprintf(&b, "- uses_types: %s\n", strings.Join(t.UsesTypes, ", "))
	}
	if len(t.Tags) > 0 {
		fmt.Fprintf(&b, "- tags: %s\n", strings.Join(t.Tags, ", "))
	}
	if t.FilePath != "" {
		fmt.Fprintf(&b, "- file_path: %s\n", t.FilePath)
	}
	b.WriteString("\n")

	if t.Description != "" {
		b.WriteString(t.Description)
		b.WriteString("\n\n")
	}
	if t.Documentation != "" {
		b.WriteString(t.Documentation)
		b.WriteString("\n\n")
	}
	if t.Code != "" {
		fmt.Fprintf(&b, "## code\n\n```%s\n%s\n```\n", langFence(t.Lang), t.Code)
	}
	if t.Examples != "" {
		fmt.Fprintf(&b, "\n## examples\n\n%s\n", t.Examples)
	}
	if t.Notes != "" {
		fmt.Fprintf(&b, "\n## notes\n\n%s\n", t.Notes)
	}
	return b.String()
}

// langFence maps registry lang codes to markdown fence labels.
func langFence(lang string) string {
	switch lang {
	case "py":
		return "python"
	case "ts":
		return "typescript"
	case "bash", "sh":
		return "bash"
	case "ps":
		return "powershell"
	default:
		return lang
	}
}

// truncate caps a string to n bytes, appending an ellipsis marker.
func truncate(s string, n int) string {
	if len(s) <= n {
		return s
	}
	return s[:n] + "\n... [truncated " + fmt.Sprintf("%d bytes]", len(s)-n)
}
