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

package mcp

import (
	"context"
	"strings"
	"testing"

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

// recipes.PAGINATION.6 api-client.LIMITS.3
func TestCallToolACIDRecipesPagination6MentionsNextPageForFullRecipeList(t *testing.T) {
	backend := &fakeBackend{recipes: []cooked.RecipeCard{
		{ID: "recipe-1", Title: "Pasta", ThumbnailURL: "https://example.invalid/thumb.jpg"},
		{ID: "recipe-2", Title: "Soup", ThumbnailURL: "https://example.invalid/thumb2.jpg"},
	}}
	server := NewServer(backend, "test")

	result, _, err := server.callReadTool(
		context.Background(),
		nil,
		ReadArguments{Target: "recipes", Page: 2, Limit: 2},
	)
	if err != nil {
		t.Fatalf("callReadTool() error = %v", err)
	}

	text := requireTextContent(t, result)
	if !strings.Contains(text, "More recipes may be available") {
		t.Fatalf("text = %q, want more recipes hint", text)
	}
	if !strings.Contains(text, "page 3") || !strings.Contains(text, "limit 2") {
		t.Fatalf("text = %q, want next page and limit", text)
	}
}

// recipes.PAGINATION.6 api-client.LIMITS.3
func TestCallToolACIDRecipesPagination6MentionsNextPageForTruncatedSearch(t *testing.T) {
	backend := &fakeBackend{searchRecipes: []cooked.RecipeCard{
		{ID: "recipe-1", Title: "Pasta", ThumbnailURL: "https://example.invalid/thumb.jpg"},
		{ID: "recipe-2", Title: "Tomato Pasta", ThumbnailURL: "https://example.invalid/thumb2.jpg"},
		{ID: "recipe-3", Title: "Pasta Salad", ThumbnailURL: "https://example.invalid/thumb3.jpg"},
	}}
	server := NewServer(backend, "test")

	result, output, err := server.callReadTool(
		context.Background(),
		nil,
		ReadArguments{Target: "recipes", Query: " pasta ", Page: 2, Limit: 1},
	)
	if err != nil {
		t.Fatalf("callReadTool() error = %v", err)
	}

	if len(output.Recipes) != 1 {
		t.Fatalf("structured recipes = %d, want truncated result length 1", len(output.Recipes))
	}
	text := requireTextContent(t, result)
	if !strings.Contains(text, "More matching recipes may be available") {
		t.Fatalf("text = %q, want more matching recipes hint", text)
	}
	if !strings.Contains(text, `query "pasta"`) {
		t.Fatalf("text = %q, want preserved search query", text)
	}
	if !strings.Contains(text, "page 2") || !strings.Contains(text, "limit 3") {
		t.Fatalf("text = %q, want same page and increased limit", text)
	}
	if !strings.Contains(text, "before trying page 3") {
		t.Fatalf("text = %q, want next page ordering guidance", text)
	}
}
