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

package channel

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/channel"
	"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 (
	listTool     = "channel-list"
	listIdsUsage = "Return the channels with the specified Ids"
	listShort    = "List channel information"
	listLong     = "List channel information. Use this tool to list channel information."
	listExample  = `# List my channel info
yutu channel list --mine
# List channel by handle
yutu channel list --forHandle @GoogleDevelopers
# List channel by ID in JSON format
yutu channel list --ids UC_x5XG1OV2P6uZZ5FSM9Ttw --output json
# List channels in table format
yutu channel list --maxResults 10 --output table`
)

var listInSchema = &jsonschema.Schema{
	Type:     "object",
	Required: []string{},
	Properties: map[string]*jsonschema.Schema{
		"category_id":  {Type: "string", Description: cidUsage},
		"for_handle":   {Type: "string", Description: fhUsage},
		"for_username": {Type: "string", Description: fuUsage},
		"hl":           {Type: "string", Description: hlUsage},
		"ids": {
			Type: "array", Items: &jsonschema.Schema{Type: "string"},
			Description: listIdsUsage,
			Default:     json.RawMessage(`[]`),
		},
		"managed_by_me": {Type: "boolean", Description: mbmUsage},
		"max_results": {
			Type: "number", Description: pkg.MRUsage,
			Default: json.RawMessage("5"),
			Minimum: new(float64(0)),
		},
		"mine":                       {Type: "boolean", Description: mineUsage},
		"my_subscribers":             {Type: "boolean", Description: msUsage},
		"on_behalf_of_content_owner": {Type: "string", Description: pkg.OBOCOUsage},
		"parts": {
			Type: "array", Description: pkg.PartsUsage,
			Items:   &jsonschema.Schema{Type: "string"},
			Default: json.RawMessage(`["id","snippet","status"]`),
		},
		"output": {
			Type: "string", Enum: []any{"json", "yaml", "table"},
			Description: pkg.TableUsage, Default: json.RawMessage(`"yaml"`),
		},
	},
}

func init() {
	mcp.AddTool(
		cmd.Server, &mcp.Tool{
			Name: listTool, Title: listShort, Description: listLong,
			InputSchema: listInSchema, Annotations: &mcp.ToolAnnotations{
				DestructiveHint: new(false),
				IdempotentHint:  true,
				OpenWorldHint:   new(true),
				ReadOnlyHint:    true,
			},
		}, cobramcp.GenToolHandler(
			listTool, func(input channel.Channel, writer io.Writer) error {
				return input.List(writer)
			},
		),
	)
	channelCmd.AddCommand(listCmd)

	listCmd.Flags().StringVarP(
		&categoryId, "categoryId", "g", "", cidUsage,
	)
	listCmd.Flags().StringVarP(
		&forHandle, "forHandle", "d", "", fhUsage,
	)
	listCmd.Flags().StringVarP(
		&forUsername, "forUsername", "u", "", fuUsage,
	)
	listCmd.Flags().StringVarP(&hl, "hl", "l", "", hlUsage)
	listCmd.Flags().StringSliceVarP(&ids, "ids", "i", []string{}, listIdsUsage)
	listCmd.Flags().BoolVarP(
		managedByMe, "managedByMe", "E", false, mbmUsage,
	)
	listCmd.Flags().Int64VarP(
		&maxResults, "maxResults", "n", 5, pkg.MRUsage,
	)
	listCmd.Flags().BoolVarP(mine, "mine", "M", true, mineUsage)
	listCmd.Flags().BoolVarP(
		mySubscribers, "mySubscribers", "S", false, msUsage,
	)
	listCmd.Flags().StringVarP(
		&onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", pkg.OBOCOUsage,
	)
	listCmd.Flags().StringSliceVarP(
		&parts, "parts", "p", []string{"id", "snippet", "status"}, pkg.PartsUsage,
	)
	listCmd.Flags().StringP("output", "o", "table", pkg.TableUsage)
}

var listCmd = &cobra.Command{
	Use:     "list",
	Short:   listShort,
	Long:    listLong,
	Example: listExample,
	Run: func(cmd *cobra.Command, args []string) {
		output, _ := cmd.Flags().GetString("output")
		input := channel.NewChannel(
			channel.WithCategoryId(categoryId),
			channel.WithForHandle(forHandle),
			channel.WithForUsername(forUsername),
			channel.WithHl(hl),
			channel.WithIds(ids),
			channel.WithChannelManagedByMe(managedByMe),
			channel.WithMaxResults(maxResults),
			channel.WithMine(mine),
			channel.WithMySubscribers(mySubscribers),
			channel.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
			channel.WithParts(parts),
			channel.WithOutput(output),
		)
		utils.HandleCmdError(input.List(cmd.OutOrStdout()), cmd)
	},
}
