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

package watermark

import (
	"io"

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

const (
	unsetTool    = "watermark-unset"
	unsetShort   = "Unset a watermark for channel's videos"
	unsetLong    = "Unset a watermark for channel's videos. Use this tool to unset a watermark for a channel's videos."
	unsetExample = `# Unset watermark for a channel
yutu watermark unset --channelId UC_x5XG1OV2P6uZZ5FSM9Ttw`
)

var unsetInSchema = &jsonschema.Schema{
	Type:     "object",
	Required: []string{"channel_id"},
	Properties: map[string]*jsonschema.Schema{
		"channel_id": {Type: "string", Description: cidUsage},
	},
}

func init() {
	mcp.AddTool(
		cmd.Server, &mcp.Tool{
			Name: unsetTool, Title: unsetShort, Description: unsetLong,
			InputSchema: unsetInSchema, Annotations: &mcp.ToolAnnotations{
				DestructiveHint: new(true),
				IdempotentHint:  true,
				OpenWorldHint:   new(true),
				ReadOnlyHint:    false,
			},
		}, cobramcp.GenToolHandler(
			unsetTool, func(input watermark.Watermark, writer io.Writer) error {
				return input.Unset(writer)
			},
		),
	)
	watermarkCmd.AddCommand(unsetCmd)

	unsetCmd.Flags().StringVarP(&channelId, "channelId", "c", "", cidUsage)
	_ = unsetCmd.MarkFlagRequired("channelId")
	cmd.AddMutationFlags(unsetCmd)
}

var unsetCmd = &cobra.Command{
	Use:     "unset",
	Short:   unsetShort,
	Long:    unsetLong,
	Example: unsetExample,
	Run: func(c *cobra.Command, args []string) {
		err := cmd.Confirm(c, "Would unset watermark for channel: %s", channelId)
		if err != nil {
			utils.HandleCmdError(err, c)
			return
		}
		input := watermark.NewWatermark(watermark.WithChannelId(channelId))
		utils.HandleCmdError(input.Unset(c.OutOrStdout()), c)
	},
}
