package convert

import (
	"encoding/json"
	"strings"

	"code.gitea.io/gitea/models/organization"
	"code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/webhook"
	gitea_convert "code.gitea.io/gitea/modules/convert"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/util"
	api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
)

func ToTag(repo *repo.Repository, gitRepo *git.Repository, t *git.Tag) (tag *api.Tag, err error) {
	tagCommit, err := ToTagCommit(repo, gitRepo, t)
	if err != nil {
		return &api.Tag{}, err
	}
	return &api.Tag{
		Name:       t.Name,
		Message:    strings.TrimSpace(t.Message),
		ID:         t.ID.String(),
		Commit:     tagCommit,
		Tagger:     gitea_convert.ToCommitUser(t.Tagger),
		ZipballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".zip"),
		TarballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".tar.gz"),
	}, nil
}

func ToTagCommit(repo *repo.Repository, gitRepo *git.Repository, t *git.Tag) (result *api.TagCommit, err error) {
	commit, err := t.Commit(gitRepo)
	if err != nil {
		return &api.TagCommit{}, nil
	}

	return &api.TagCommit{
		CommitMeta: gitea_convert.ToCommitMeta(repo, t),
		Committer:  gitea_convert.ToCommitUser(commit.Committer),
		Author:     gitea_convert.ToCommitUser(commit.Author),
		Message:    commit.CommitMessage,
	}, nil
}

func ToOrganization(org *organization.Organization, team *organization.Team) (*api.Organization, error) {
	apiTeam, err := gitea_convert.ToTeam(team)
	if err != nil {
		return &api.Organization{}, err
	}
	return &api.Organization{
		ID:                        org.ID,
		Name:                      org.Name,
		AvatarURL:                 org.AvatarLink(),
		UserName:                  org.Name,
		FullName:                  org.FullName,
		Description:               org.Description,
		Website:                   org.Website,
		Location:                  org.Location,
		Visibility:                org.Visibility.String(),
		RepoAdminChangeTeamAccess: org.RepoAdminChangeTeamAccess,
		OwnerTeam:                 apiTeam,
	}, nil
}

func ToHookTask(t *webhook.HookTask) *api.HookTask {
	config := map[string]string{
		"url":          t.RequestInfo.URL,
		"content_type": t.RequestInfo.Headers["Content-Type"],
		"http_method":  t.RequestInfo.HTTPMethod,
	}

	payloadContent := make(map[string]interface{})
	requestContent := make(map[string]interface{})
	responseContent := make(map[string]interface{})
	_ = json.Unmarshal([]byte(t.PayloadContent), &payloadContent)
	_ = json.Unmarshal([]byte(t.RequestContent), &requestContent)
	_ = json.Unmarshal([]byte(t.ResponseContent), &responseContent)

	return &api.HookTask{
		ID:              t.ID,
		UUID:            t.UUID,
		Type:            string(t.EventType),
		Config:          config,
		PayloadContent:  payloadContent,
		EventType:       string(t.EventType),
		IsDelivered:     t.IsDelivered,
		Delivered:       t.Delivered,
		IsSucceed:       t.IsSucceed,
		RequestContent:  requestContent,
		ResponseContent: responseContent,
	}
}
