// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: LicenseRef-MutuaL-1.2

package mcp

import (
	"fmt"
	"strconv"
	"strings"

	"git.secluded.site/cooked-mcp/internal/cooked"
)

func formatRecipes(recipes []cooked.RecipeCard) string {
	if len(recipes) == 0 {
		return "No saved recipes found."
	}

	var builder strings.Builder
	builder.WriteString("Saved recipes:\n")
	for _, recipe := range recipes {
		builder.WriteString("- ")
		builder.WriteString(recipe.Title)
		builder.WriteString(" (id: ")
		builder.WriteString(recipe.ID)
		builder.WriteString(")\n")
	}

	return strings.TrimRight(builder.String(), "\n")
}

func formatRecipeNextPageHint(query string, nextPage, limit int) string {
	if query == "" {
		return fmt.Sprintf(
			"More recipes may be available. Call read with target recipes, page %d, limit %d to continue.",
			nextPage,
			limit,
		)
	}

	return fmt.Sprintf(
		"More matching recipes may be available. Call read with target recipes, query %q, page %d, limit %d to continue.",
		query,
		nextPage,
		limit,
	)
}

func formatRecipeSearchLimitHint(query string, page, nextPage, limit int) string {
	return fmt.Sprintf(
		"More matching recipes may be available. Call read with target recipes, query %q, page %d, limit %d to include more results from this page before trying page %d.",
		query,
		page,
		limit,
		nextPage,
	)
}

func formatRecipe(recipe RecipeDetail) string {
	title := recipe.Title
	if title == "" {
		title = "(untitled recipe)"
	}

	var builder strings.Builder
	builder.WriteString("Recipe: ")
	builder.WriteString(title)
	builder.WriteString(" (id: ")
	builder.WriteString(recipe.ID)
	builder.WriteString(")\n")
	if recipe.Owner != "" {
		builder.WriteString("Owner: ")
		builder.WriteString(recipe.Owner)
		builder.WriteByte('\n')
	}
	fmt.Fprintf(&builder, "Portions: %s\n", formatPortions(recipe.Portions))

	content := strings.TrimSpace(recipe.Content)
	if content == "" {
		builder.WriteString("\nNo recipe content returned.")
	} else {
		builder.WriteString("\n")
		builder.WriteString(content)
	}

	return strings.TrimRight(builder.String(), "\n")
}

func formatRecipeTextPreview(preview ExtractIntoRecipeOutput) string {
	title := preview.Title
	if title == "" {
		title = "(untitled extraction)"
	}

	var builder strings.Builder
	builder.WriteString("Extracted recipe text: ")
	builder.WriteString(title)
	builder.WriteByte('\n')
	fmt.Fprintf(&builder, "Portions: %s\n", formatPortions(preview.Portions))

	markdown := strings.TrimSpace(preview.Markdown)
	if markdown == "" {
		builder.WriteString("\nNo preview markdown returned.")
	} else {
		builder.WriteString("\n")
		builder.WriteString(markdown)
	}

	return strings.TrimRight(builder.String(), "\n")
}

func formatPortions(portions float64) string { return strconv.FormatFloat(portions, 'f', -1, 64) }

func formatRecipeSave(output SaveRecipeOutput) string {
	return "Saved recipe (id: " + output.RecipeID + ")."
}

func formatRecipeOverwrite(output SaveRecipeOutput) string {
	return "Saved draft into existing recipe (id: " + output.RecipeID + ")."
}

func formatRecipeURLImportDraft(output SaveRecipeOutput) string {
	title := output.Title
	if strings.TrimSpace(title) == "" {
		title = "(untitled draft)"
	}

	var builder strings.Builder
	builder.WriteString("Imported recipe URL as draft: ")
	builder.WriteString(title)
	builder.WriteString(" (draft_id: ")
	builder.WriteString(output.DraftID)
	builder.WriteString("). Review the markdown and portions before saving.")
	if output.Portions > 0 {
		fmt.Fprintf(&builder, "\nPortions: %s", formatPortions(output.Portions))
	}
	if strings.TrimSpace(output.Markdown) != "" {
		builder.WriteString("\n\n")
		builder.WriteString(output.Markdown)
	}

	return strings.TrimRight(builder.String(), "\n")
}

func formatRecipeDelete(output DeleteRecipeOutput) string {
	return "Deleted recipe (id: " + output.RecipeID + ")."
}
