/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli

import (
	"context"
	"fmt"

	"github.com/spf13/cobra"
	"go.unistack.org/micro/v4/logger"
)

// updateCmd represents the update command
var listCmd = NewListCommand()

var prList = map[string]map[string]string{}

/* [
	"master":
		[
			"go.unistack.org/micro/v4" : "Bump go.unistack.org/micro/v4 from v4.0.0 to v4.0.1",
			"go.unistack.org/micro-client-http/v4":"Bump go.unistack.org/micro-client-http/v4 from v4.0.0 to v4.0.2",
		],
	"v3":
		[
			"go.unistack.org/micro/v3" : "Bump go.unistack.org/micro/v4 from v3.0.0 to v3.0.1",
		],
] */

func init() {
	rootCmd.AddCommand(listCmd)
}

func NewListCommand() *cobra.Command {
	ctx := context.Background()

	logger.Info(ctx, "RequestList start")

	cmd := &cobra.Command{
		Use:   "list",
		Short: "Update allows you to start the process of creating a PR in the repository",
		Long: `
Update allows you to start the process of creating a PR in the repository. 
Use the -a flag to update all dependencies or -m flag a specific module.
`,
		RunE: func(cmd *cobra.Command, args []string) error {
			if gitsource == nil {
				return errSourceNil
			}

			if cfg == nil {
				return errCfgNil
			}

			prList = make(map[string]map[string]string)

			for _, branch := range cfg.Branches {
				rMap, err := gitsource.RequestList(ctx, branch)
				if err != nil {
					return err
				}
				prList[branch] = rMap

				logger.Info(ctx, fmt.Sprintf("for %s:\n%s", branch, rMap))
			}

			return nil
		},
	}

	return cmd
}
