package cmd

import (
	"context"
	"fmt"

	"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
	"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
	"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
	"github.com/spf13/cobra"
)

var appRemoveCmd = &cobra.Command{
	Use:   "remove <app_client_id>",
	Short: "Remove an app",
	Long:  `Remove the app with the given ID from the database.`,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		removeApp(args[0])
	},
}

func removeApp(appID string) {
	c := utils.InitConfig("")
	logger.Init(c.LogLevel)

	s, err := db.New(*c)
	if err != nil {
		utils.Failf("failed to init storage: %s", err.Error())
	}

	if err := s.ClientStorage().DeleteClient(context.Background(), appID); err != nil {
		utils.Failf("Failed to remove app: %s", err.Error())
	}
	fmt.Println("App deleted")
}

func init() {
	appCmd.AddCommand(appRemoveCmd)
}
