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

package task

import (
	"context"

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

// DeleteToolName is the name of the delete task tool.
const DeleteToolName = "delete_task"

// DeleteToolDescription describes the delete task tool for LLMs.
const DeleteToolDescription = `Deletes a task from Lunatask.

Required:
- id: Task UUID or lunatask:// deep link

This action is permanent and cannot be undone.`

// DeleteInput is the input schema for deleting a task.
type DeleteInput struct {
	ID string `json:"id" jsonschema:"required"`
}

// DeleteOutput is the output schema for deleting a task.
type DeleteOutput struct {
	Success  bool   `json:"success"`
	DeepLink string `json:"deep_link"`
}

// HandleDelete deletes a task.
func (h *Handler) HandleDelete(
	ctx context.Context,
	_ *mcp.CallToolRequest,
	input DeleteInput,
) (*mcp.CallToolResult, DeleteOutput, error) {
	_, id, err := lunatask.ParseReference(input.ID)
	if err != nil {
		return shared.ErrorResult("invalid ID: expected UUID or lunatask:// deep link"), DeleteOutput{}, nil
	}

	deepLink, _ := lunatask.BuildDeepLink(lunatask.ResourceTask, id)

	if _, err := h.client.DeleteTask(ctx, id); err != nil {
		return shared.ErrorResult(err.Error()), DeleteOutput{}, nil
	}

	return &mcp.CallToolResult{
		Content: []mcp.Content{&mcp.TextContent{
			Text: "Task deleted: " + deepLink,
		}},
	}, DeleteOutput{Success: true, DeepLink: deepLink}, nil
}
