package fs

import (
	"fmt"
	"os"

	"codeberg.org/shroff/phylum/server/internal/command/common"
	"github.com/spf13/cobra"
)

func setupDeleteCommand() *cobra.Command {
	cmd := cobra.Command{
		Use:     "delete <path>",
		Aliases: []string{"rm"},
		Short:   "Delete Resource",
		Args:    cobra.ExactArgs(1),
		Run: func(cmd *cobra.Command, args []string) {
			f := common.UserFileSystem(cmd)
			pathOrUUID := args[0]
			r, err := f.ResourceByPathWithRoot(pathOrUUID)
			if err != nil {
				fmt.Println("cannot remove '" + pathOrUUID + "': " + err.Error())
				os.Exit(1)
			}

			if r.Dir() {
				if recursive, err := cmd.Flags().GetBool("recursive"); err != nil || !recursive {
					fmt.Println("cannot remove '" + pathOrUUID + "': Is a directory. Try again with '-r'")
					os.Exit(1)
				}
			}

			hard, _ := cmd.Flags().GetBool("hard")
			if hard {
				err = f.DeleteForever(r)
			} else {
				_, err = f.Delete(r)
			}
			if err != nil {
				fmt.Println("cannot remove '" + pathOrUUID + "': " + err.Error())
				os.Exit(1)
			}
		},
	}
	cmd.Flags().BoolP("recursive", "r", false, "Recursive (required for directories)")
	cmd.Flags().BoolP("hard", "x", false, "Permanently delete (do not send to trash)")

	return &cmd
}
