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

import (
	"context"
	"fmt"

	"github.com/spf13/cobra"
)

// deleteCmd represents the delete command
var deleteCmd = NewDeleteCommand()

type DeleteFlags struct {
	all bool
	mod string
}

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

func NewDeleteCommand() *cobra.Command {
	var flags UpdateFlags

	ctx := context.Background()

	cmd := &cobra.Command{
		Use:   "delete",
		Short: "Delete closes the merge requests created with a dependency update.",
		Long:  `Delete closes the merge requests created with a dependency update.`,
		RunE: func(cmd *cobra.Command, args []string) error {
			fmt.Println("delete called")
			var err error

			if gitsource == nil {
				return errSourceNil
			}

			if len(prList) == 0 {
				return errPRNotExist
			}

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

			if flags.mod != "" {
				for _, branch := range cfg.Branches {
					err = gitsource.RequestClose(ctx, branch, flags.mod)
					if err != nil {
						return err
					}
				}
			}

			return errFlagsNotExist
		},
	}

	deleteCmd.Flags().BoolVarP(&flags.all, "all", "a", false, "Deletes everything depending")
	deleteCmd.Flags().StringVarP(&flags.mod, "mod", "m", "", "Deletes one dependency")

	return cmd
}
