/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cli

import (
	"context"
	"fmt"
	"path/filepath"

	"git.unistack.org/unistack-org/pkgdash/internal/configcli"
	"git.unistack.org/unistack-org/pkgdash/internal/source"
	"github.com/spf13/cobra"
	yamlcodec "go.unistack.org/micro-codec-yaml/v4"
	envconfig "go.unistack.org/micro-config-env/v4"
	fileconfig "go.unistack.org/micro-config-file/v4"
	"go.unistack.org/micro/v4/config"
	"go.unistack.org/micro/v4/logger"
	"go.unistack.org/micro/v4/logger/slog"
	"go.unistack.org/micro/v4/options"
)

// initCmd represents the init command
var initCmd = NewInitCommand()

var gitsource = source.SourceControl(nil)

var cfg = configcli.NewConfig()

var (
	DefaultPullRequestTitle = `Bump {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
	DefaultPullRequestBody  = `Bumps {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
)

var (
	configFiles = []string{
		"dependabot.yml",
		"pkgdashcli.yml",
		"pkgdashcli.yaml",
	}
	configDirs = []string{
		".gitea",
		".github",
		".gitlab",
	}
)

func init() {
	rootCmd.AddCommand(initCmd)
}

func NewInitCommand() *cobra.Command {
	ctx := context.Background()

	logger.DefaultLogger = slog.NewLogger()

	if err := logger.DefaultLogger.Init(logger.WithCallerSkipCount(3), logger.WithLevel(logger.DebugLevel)); err != nil {
		logger.Error(ctx, fmt.Sprintf("logger init error: %v", err))
	}

	cmd := &cobra.Command{
		Use:   "init",
		Short: "Init fills the config with data from the configuration file.",
		Long:  `Init fills the config with data from the configuration file.`,
		RunE: func(cmd *cobra.Command, args []string) error {
			fmt.Println("init called")
			var err error

			if err = config.Load(ctx,
				[]config.Config{
					config.NewConfig(
						config.Struct(cfg),
					),
					envconfig.NewConfig(
						config.Struct(cfg),
					),
				},
				config.LoadOverride(true),
			); err != nil {
				logger.Fatal(ctx, fmt.Sprintf("failed to load config: %v", err))
			}

			for _, configDir := range configDirs {
				for _, configFile := range configFiles {
					logger.Info(ctx, fmt.Sprintf("path: %s", filepath.Join(configDir, configFile)))
					c := fileconfig.NewConfig(
						config.AllowFail(false),
						config.Struct(cfg),
						options.Codec(yamlcodec.NewCodec()),
						fileconfig.Path(".gitea/pkgdashcli.yaml"),
					)
					err = c.Init(options.Context(ctx))
					if err != nil {
						logger.Error(ctx, fmt.Sprintf("failed to init config: %v", err))
						return err
					}
					if err = c.Load(ctx, config.LoadOverride(true)); err != nil {
						logger.Error(ctx, fmt.Sprintf("failed to load config: %v", err))
						return err
					}
				}
			}

			logger.Info(ctx, fmt.Sprintf("Load config... %s", cfg.Source.Repository))

			if cfg.PullRequestBody == "" {
				cfg.PullRequestBody = DefaultPullRequestBody
			}

			if cfg.PullRequestTitle == "" {
				cfg.PullRequestTitle = DefaultPullRequestTitle
			}

			gitsource = source.NewSourceControl(*cfg)

			return nil
		},
	}

	return cmd
}
