package git

import (
	"fmt"

	"coopcloud.tech/abra/pkg/log"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
)

// Init inits a new repo and commits all the stuff if you want
func Init(repoPath string, commit bool, gitName, gitEmail string) error {
	repo, err := git.PlainInit(repoPath, false)
	if err != nil {
		return fmt.Errorf("git init: %s", err)
	}

	if err = SwitchToMain(repo); err != nil {
		return fmt.Errorf("git branch rename: %s", err)
	}

	log.Debugf("initialised new git repo in %s", repoPath)

	if commit {
		commitRepo, err := git.PlainOpen(repoPath)
		if err != nil {
			return fmt.Errorf("git open: %s", err)
		}

		commitWorktree, err := commitRepo.Worktree()
		if err != nil {
			return fmt.Errorf("git worktree: %s", err)
		}

		if err := commitWorktree.AddWithOptions(&git.AddOptions{All: true}); err != nil {
			return fmt.Errorf("git add: %s", err)
		}

		var author *object.Signature
		if gitName != "" && gitEmail != "" {
			author = &object.Signature{Name: gitName, Email: gitEmail}
		}

		if _, err = commitWorktree.Commit("init", &git.CommitOptions{Author: author}); err != nil {
			return fmt.Errorf("git commit: %s", err)
		}

		log.Debugf("init committed all files for new git repo in %s", repoPath)
	}

	return nil
}

// SwitchToMain sets the default branch to "main".
func SwitchToMain(repo *git.Repository) error {
	ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main"))
	if err := repo.Storer.SetReference(ref); err != nil {
		return fmt.Errorf("set reference: %s", err)
	}

	cfg, err := repo.Config()
	if err != nil {
		return fmt.Errorf("repo config: %s", err)
	}

	cfg.Init.DefaultBranch = "main"
	if err := repo.SetConfig(cfg); err != nil {
		return fmt.Errorf("repo set config: %s", err)
	}

	log.Debug("set 'main' as the default branch")

	return nil
}