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

package mcp

import (
	"context"
	"testing"

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

// recipes.SAVE.3 recipes.SAVE.4 recipes.SAVE.9 tools.SAVE_RECIPE_TOOL.1
func TestCallToolACIDRecipesSave3And4And9ImportsURLSavedRecipe(t *testing.T) {
	backend := &fakeBackend{importRecipeURLResult: cooked.RecipeURLImport{RecipeID: "recipe-1"}}
	server := NewServer(backend, "test")

	_, output, err := server.callSaveRecipeTool(
		context.Background(),
		nil,
		SaveRecipeArguments{Source: " url ", URL: " https://example.invalid/recipe "},
	)
	if err != nil {
		t.Fatalf("callSaveRecipeTool() error = %v", err)
	}

	if backend.importRecipeURLCalls != 1 {
		t.Fatalf("import calls = %d, want 1", backend.importRecipeURLCalls)
	}
	if backend.importURL != "https://example.invalid/recipe" {
		t.Fatalf("backend import URL = %q, want trimmed URL", backend.importURL)
	}
	if output.RecipeID != "recipe-1" || output.DraftID != "" {
		t.Fatalf("save output = %#v, want recipe_id only", output)
	}
	if backend.metadataCalls != 0 || backend.contentCalls != 0 {
		t.Fatalf("draft read calls = metadata %d content %d, want none", backend.metadataCalls, backend.contentCalls)
	}
	if backend.saveCalls != 0 || backend.updateCalls != 0 {
		t.Fatalf("save/update calls = %d/%d, want none", backend.saveCalls, backend.updateCalls)
	}
}

// recipes.SAVE.3-1 tools.SAVE_RECIPE_TOOL.4
func TestCallToolACIDRecipesSave3_1AndToolsSaveRecipeTool4RequiresURL(t *testing.T) {
	backend := &fakeBackend{}
	server := NewServer(backend, "test")

	_, _, err := server.callSaveRecipeTool(context.Background(), nil, SaveRecipeArguments{Source: "url", URL: "   "})
	if err == nil {
		t.Fatal("callSaveRecipeTool() error = nil, want missing URL error")
	}
	if backend.importRecipeURLCalls != 0 {
		t.Fatalf("import calls = %d, want none", backend.importRecipeURLCalls)
	}
}

// recipes.SAVE.5 recipes.SAVE.6 recipes.SAVE.12 recipes.SAVE.14 recipes.SAVE.15 recipes.SAVE.16
func TestCallToolACIDRecipesSave5And12And14And15ReturnsURLImportDraft(t *testing.T) {
	backend := &fakeBackend{
		importRecipeURLResult: cooked.RecipeURLImport{DraftID: "draft-1"},
		recipeMetadata:        cooked.RecipeMetadata{Title: "Pasta"},
		recipeContent:         cooked.RecipeContent{Content: "# Pasta\n\n1. Boil pasta.", Portions: 2},
	}
	server := NewServer(backend, "test")

	_, output, err := server.callSaveRecipeTool(
		context.Background(),
		nil,
		SaveRecipeArguments{Source: "url", URL: " https://example.invalid/pasta "},
	)
	if err != nil {
		t.Fatalf("callSaveRecipeTool() error = %v", err)
	}

	if backend.importRecipeURLCalls != 1 {
		t.Fatalf("import calls = %d, want 1", backend.importRecipeURLCalls)
	}
	if backend.importURL != "https://example.invalid/pasta" {
		t.Fatalf("backend import URL = %q, want trimmed URL", backend.importURL)
	}
	if backend.metadataRecipeID != "draft-1" || backend.contentRecipeID != "draft-1" {
		t.Fatalf(
			"draft read IDs = metadata %q content %q, want draft-1",
			backend.metadataRecipeID,
			backend.contentRecipeID,
		)
	}
	if backend.metadataCalls != 1 || backend.contentCalls != 1 {
		t.Fatalf("draft read calls = metadata %d content %d, want 1/1", backend.metadataCalls, backend.contentCalls)
	}
	if output.RecipeID != "" || output.DraftID != "draft-1" || output.Title != "Pasta" {
		t.Fatalf("draft output identity = %#v, want draft-1 Pasta with no recipe ID", output)
	}
	if output.Markdown != "# Pasta\n\n1. Boil pasta." || output.Portions != 2 {
		t.Fatalf("draft output content = %q/%g, want markdown/2", output.Markdown, output.Portions)
	}
	if backend.saveCalls != 0 || backend.updateCalls != 0 {
		t.Fatalf("save/update calls = %d/%d, want none", backend.saveCalls, backend.updateCalls)
	}
}
