package show

import (
	"fmt"
	"os"

	"github.com/ardanlabs/kronk/cmd/kronk/client"
	"github.com/ardanlabs/kronk/sdk/kronk"
	"github.com/ardanlabs/kronk/sdk/tools/defaults"
	"github.com/ardanlabs/kronk/sdk/tools/models"
	"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
	Use:   "show <MODEL_NAME>",
	Short: "Show information for a model",
	Long: `Show information for a model

Environment Variables (web mode - default):
      KRONK_TOKEN         (required when auth enabled)  Authentication token for the kronk server.
      KRONK_WEB_API_HOST  (default localhost:11435)  IP Address for the kronk server.

Environment Variables (--local mode):
      KRONK_BASE_PATH  Base path for kronk data (models, libraries, catalog, model_config)
      KRONK_MODELS     (default: $HOME/.kronk/models)  The path to the models directory`,
	Args: cobra.ExactArgs(1),
	Run:  main,
}

func init() {
	Cmd.Flags().Bool("local", false, "Run without the model server")
}

func main(cmd *cobra.Command, args []string) {
	if err := run(cmd, args); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func run(cmd *cobra.Command, args []string) error {
	local, _ := cmd.Flags().GetBool("local")

	mdls, err := models.NewWithPaths(client.GetBasePath(cmd))
	if err != nil {
		return fmt.Errorf("unable to create models system: %w", err)
	}

	switch local {
	case true:
		if ierr := kronk.Init(); ierr != nil {
			return fmt.Errorf("unable to init kronk: %w", ierr)
		}

		modelConfigFile, ferr := defaults.ModelConfigFile("", client.GetBasePath(cmd))
		if ferr != nil {
			return fmt.Errorf("resolving model config file: %w", ferr)
		}

		mc, lerr := models.LoadModelConfig(modelConfigFile)
		if lerr != nil {
			return fmt.Errorf("loading model config: %w", lerr)
		}

		err = runLocal(mdls, mc, args)
	default:
		err = runWeb(args)
	}

	if err != nil {
		return err
	}

	return nil
}
