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

package video

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/utils"
	"github.com/eat-pray-ai/yutu/pkg/video"
	"github.com/google/jsonschema-go/jsonschema"
	"github.com/modelcontextprotocol/go-sdk/mcp"
	"github.com/spf13/cobra"
)

const (
	getRatingTool    = "video-getRating"
	grIdsUsage       = "IDs of the videos to get the rating for"
	getRatingShort   = "Get video ratings"
	getRatingLong    = "Get video ratings. Use this tool to get video ratings by IDs."
	getRatingExample = `# Get rating of a video
yutu video getRating --ids dQw4w9WgXcQ
# Get ratings of multiple videos in JSON format
yutu video getRating --ids dQw4w9WgXcQ,abc123 --output json`
)

var getRatingInSchema = &jsonschema.Schema{
	Type:     "object",
	Required: []string{"ids"},
	Properties: map[string]*jsonschema.Schema{
		"ids": {
			Type: "array", Description: grIdsUsage,
			Items: &jsonschema.Schema{Type: "string"},
		},
		"on_behalf_of_content_owner": {Type: "string", Description: pkg.OBOCOUsage},
		"output": {
			Type: "string", Enum: []any{"json", "yaml", "table"},
			Description: pkg.TableUsage, Default: json.RawMessage(`"yaml"`),
		},
	},
}

func init() {
	mcp.AddTool(
		cmd.Server, &mcp.Tool{
			Name: getRatingTool, Title: getRatingShort, Description: getRatingLong,
			InputSchema: getRatingInSchema, Annotations: &mcp.ToolAnnotations{
				DestructiveHint: new(false),
				IdempotentHint:  true,
				OpenWorldHint:   new(true),
				ReadOnlyHint:    true,
			},
		}, cobramcp.GenToolHandler(
			getRatingTool, func(input video.Video, writer io.Writer) error {
				return input.GetRating(writer)
			},
		),
	)
	videoCmd.AddCommand(getRatingCmd)

	getRatingCmd.Flags().StringSliceVarP(&ids, "ids", "i", []string{}, grIdsUsage)
	getRatingCmd.Flags().StringVarP(
		&onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", pkg.OBOCOUsage,
	)
	getRatingCmd.Flags().StringP("output", "o", "", pkg.TableUsage)

	_ = getRatingCmd.MarkFlagRequired("ids")
}

var getRatingCmd = &cobra.Command{
	Use:     "getRating",
	Short:   getRatingShort,
	Long:    getRatingLong,
	Example: getRatingExample,
	Run: func(cmd *cobra.Command, args []string) {
		output, _ := cmd.Flags().GetString("output")
		input := video.NewVideo(
			video.WithIds(ids),
			video.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
			video.WithOutput(output),
			video.WithService(nil),
		)
		utils.HandleCmdError(input.GetRating(cmd.OutOrStdout()), cmd)
	},
}
