package recipe

import (
	"path"

	"coopcloud.tech/abra/cli/internal"
	"coopcloud.tech/abra/pkg/autocomplete"
	"coopcloud.tech/abra/pkg/config"
	gitPkg "coopcloud.tech/abra/pkg/git"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli"
)

var recipeDiffCommand = cli.Command{
	Name:        "diff",
	Usage:       "Show unstaged changes in recipe config",
	Description: "Due to limitations in our underlying Git dependency, this command requires /usr/bin/git.",
	Aliases:     []string{"d"},
	ArgsUsage:   "<recipe>",
	Flags: []cli.Flag{
		internal.DebugFlag,
		internal.NoInputFlag,
	},
	Before:       internal.SubCommandBefore,
	BashComplete: autocomplete.RecipeNameComplete,
	Action: func(c *cli.Context) error {
		recipeName := c.Args().First()

		if recipeName != "" {
			internal.ValidateRecipe(c)
		}

		recipeDir := path.Join(config.RECIPES_DIR, recipeName)
		if err := gitPkg.DiffUnstaged(recipeDir); err != nil {
			logrus.Fatal(err)
		}

		return nil
	},
}