// Copyright 2025 eat-pray-ai & OpenWaygate
// SPDX-License-Identifier: Apache-2.0

package thumbnail

import (
	"encoding/json"
	"io"

	cobramcp "github.com/eat-pray-ai/cobra-mcp"
	"github.com/eat-pray-ai/yutu/cmd"
	"github.com/eat-pray-ai/yutu/pkg"
	"github.com/eat-pray-ai/yutu/pkg/thumbnail"
	"github.com/eat-pray-ai/yutu/pkg/utils"
	"github.com/google/jsonschema-go/jsonschema"
	"github.com/modelcontextprotocol/go-sdk/mcp"
	"github.com/spf13/cobra"
)

const (
	setTool    = "thumbnail-set"
	setShort   = "Set a thumbnail for a video"
	setLong    = "Set a thumbnail for a video. Use this tool to set a thumbnail for a video."
	setExample = `# Set a thumbnail for a video
yutu thumbnail set --file image.jpg --videoId dQw4w9WgXcQ
# Set a thumbnail with JSON output
yutu thumbnail set --file image.png --videoId dQw4w9WgXcQ --output json`
)

var setInSchema = &jsonschema.Schema{
	Type:     "object",
	Required: []string{"file", "video_id"},
	Properties: map[string]*jsonschema.Schema{
		"file":     {Type: "string", Description: fileUsage},
		"video_id": {Type: "string", Description: vidUsage},
		"output": {
			Type: "string", Enum: []any{"json", "yaml", "silent"},
			Description: pkg.SilentUsage, Default: json.RawMessage(`"yaml"`),
		},
	},
}

func init() {
	mcp.AddTool(
		cmd.Server, &mcp.Tool{
			Name: setTool, Title: setShort, Description: setLong,
			InputSchema: setInSchema, Annotations: &mcp.ToolAnnotations{
				DestructiveHint: new(false),
				IdempotentHint:  true,
				OpenWorldHint:   new(true),
				ReadOnlyHint:    false,
			},
		}, cobramcp.GenToolHandler(
			setTool, func(input thumbnail.Thumbnail, writer io.Writer) error {
				return input.Set(writer)
			},
		),
	)
	thumbnailCmd.AddCommand(setCmd)

	setCmd.Flags().StringVarP(&file, "file", "f", "", fileUsage)
	setCmd.Flags().StringVarP(&videoId, "videoId", "v", "", vidUsage)
	setCmd.Flags().StringP("output", "o", "", pkg.SilentUsage)

	_ = setCmd.MarkFlagRequired("file")
	_ = setCmd.MarkFlagRequired("videoId")
	cmd.AddMutationFlags(setCmd)
}

var setCmd = &cobra.Command{
	Use:     "set",
	Short:   setShort,
	Long:    setLong,
	Example: setExample,
	Run: func(c *cobra.Command, args []string) {
		output, _ := c.Flags().GetString("output")
		err := cmd.Confirm(c, "Would set thumbnail for video: %s", videoId)
		if err != nil {
			utils.HandleCmdError(err, c)
			return
		}
		input := thumbnail.NewThumbnail(
			thumbnail.WithFile(file),
			thumbnail.WithVideoId(videoId),
			thumbnail.WithOutput(output),
		)
		utils.HandleCmdError(input.Set(c.OutOrStdout()), c)
	},
}
