// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package person

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"git.secluded.site/lune/internal/ui"
	"git.secluded.site/lune/internal/validate"
	"github.com/spf13/cobra"
)

// DeleteCmd deletes a person. Exported for potential use by shortcuts.
var DeleteCmd = &cobra.Command{
	Use:   "delete ID",
	Short: "Delete a person",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		if err := validate.ID(args[0]); err != nil {
			return err
		}

		force, _ := cmd.Flags().GetBool("force")
		if !force {
			fmt.Fprintf(cmd.OutOrStderr(), "%s Delete person %s? [y/N] ",
				ui.Warning.Render("Warning:"), args[0])

			reader := bufio.NewReader(os.Stdin)
			response, _ := reader.ReadString('\n')
			response = strings.TrimSpace(strings.ToLower(response))

			if response != "y" && response != "yes" {
				fmt.Fprintln(cmd.OutOrStdout(), "Cancelled")

				return nil
			}
		}

		// TODO: implement person deletion
		fmt.Fprintf(cmd.OutOrStdout(), "Deleting person %s (not yet implemented)\n", args[0])

		return nil
	},
}

func init() {
	DeleteCmd.Flags().BoolP("force", "f", false, "Skip confirmation prompt")
}
