// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

// Package note provides MCP tools for Lunatask note operations.
package note

import (
	"context"

	"git.secluded.site/go-lunatask"
	"git.secluded.site/lune/internal/mcp/shared"
	"github.com/modelcontextprotocol/go-sdk/mcp"
)

// CreateToolName is the name of the create note tool.
const CreateToolName = "create_note"

// CreateToolDescription describes the create note tool for LLMs.
const CreateToolDescription = `Creates a new note in Lunatask.

Optional:
- name: Note title
- notebook_id: Notebook UUID (get from lunatask://notebooks resource)
- content: Markdown content
- source: Origin identifier for integrations
- source_id: Source-specific ID (requires source)

All fields are optional — can create an empty note.
Returns the deep link to the created note.

Note: If a note with the same source/source_id already exists,
the API returns a duplicate warning instead of creating a new note.`

// CreateInput is the input schema for creating a note.
type CreateInput struct {
	Name       *string `json:"name,omitempty"`
	NotebookID *string `json:"notebook_id,omitempty"`
	Content    *string `json:"content,omitempty"`
	Source     *string `json:"source,omitempty"`
	SourceID   *string `json:"source_id,omitempty"`
}

// CreateOutput is the output schema for creating a note.
type CreateOutput struct {
	DeepLink string `json:"deep_link"`
}

// Handler handles note-related MCP tool requests.
type Handler struct {
	client    *lunatask.Client
	notebooks []shared.NotebookProvider
}

// NewHandler creates a new note handler.
func NewHandler(accessToken string, notebooks []shared.NotebookProvider) *Handler {
	return &Handler{
		client:    lunatask.NewClient(accessToken, lunatask.UserAgent("lune-mcp/1.0")),
		notebooks: notebooks,
	}
}

// HandleCreate creates a new note.
func (h *Handler) HandleCreate(
	ctx context.Context,
	_ *mcp.CallToolRequest,
	input CreateInput,
) (*mcp.CallToolResult, CreateOutput, error) {
	if input.NotebookID != nil {
		if err := lunatask.ValidateUUID(*input.NotebookID); err != nil {
			return shared.ErrorResult("invalid notebook_id: expected UUID"), CreateOutput{}, nil
		}
	}

	builder := h.client.NewNote()

	if input.Name != nil {
		builder.WithName(*input.Name)
	}

	if input.NotebookID != nil {
		builder.InNotebook(*input.NotebookID)
	}

	if input.Content != nil {
		builder.WithContent(*input.Content)
	}

	if input.Source != nil && input.SourceID != nil {
		builder.FromSource(*input.Source, *input.SourceID)
	}

	note, err := builder.Create(ctx)
	if err != nil {
		return shared.ErrorResult(err.Error()), CreateOutput{}, nil
	}

	if note == nil {
		return &mcp.CallToolResult{
			Content: []mcp.Content{&mcp.TextContent{
				Text: "Note already exists (duplicate source)",
			}},
		}, CreateOutput{}, nil
	}

	deepLink, _ := lunatask.BuildDeepLink(lunatask.ResourceNote, note.ID)

	return &mcp.CallToolResult{
		Content: []mcp.Content{&mcp.TextContent{
			Text: "Note created: " + deepLink,
		}},
	}, CreateOutput{DeepLink: deepLink}, nil
}
