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)
	}
	MasterToMain(repo)
	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
}

func MasterToMain(repo *git.Repository) {
	// Create and set the 'main' branch as default
	ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main"))
	if err := repo.Storer.SetReference(ref); err != nil {
		log.Fatalf("Failed to set 'main' as default branch: %v", err)
	}
	// Update the repository configuration
	cfg, err := repo.Config()
	if err != nil {
		log.Fatalf("Failed to get repository config: %v", err)
	}
	cfg.Init.DefaultBranch = "main"
	if err := repo.SetConfig(cfg); err != nil {
		log.Fatalf("Failed to update repository config: %v", err)
	}
	fmt.Println("Set 'main' as the default branch.")
}
