package database

import (
	"github.com/cockroachdb/errors"
	"github.com/gogs/git-module"
)

type Tag struct {
	RepoPath string
	Name     string

	IsProtected bool
	Commit      *git.Commit
}

func (ta *Tag) GetCommit() (*git.Commit, error) {
	gitRepo, err := git.Open(ta.RepoPath)
	if err != nil {
		return nil, errors.Newf("open repository: %v", err)
	}
	return gitRepo.TagCommit(ta.Name)
}

func GetTagsByPath(path string) ([]*Tag, error) {
	gitRepo, err := git.Open(path)
	if err != nil {
		return nil, errors.Newf("open repository: %v", err)
	}

	names, err := gitRepo.Tags()
	if err != nil {
		return nil, errors.Newf("list tags: %v", err)
	}

	tags := make([]*Tag, len(names))
	for i := range names {
		tags[i] = &Tag{
			RepoPath: path,
			Name:     names[i],
		}
	}
	return tags, nil
}

func (r *Repository) GetTags() ([]*Tag, error) {
	return GetTagsByPath(r.RepoPath())
}
