package github

import (
	ent "github.com/tidbcloud/devops/entity/github"
	"github.com/tidbcloud/devops/pkg/infra/clients/githubcli"
)

func ConvertPullRequest(pr githubcli.PullRequest) ent.PullRequest {
	return ent.PullRequest{
		PullRequestInDB: ent.PullRequestInDB{
			ID:        0,
			Location:  ent.NewPRLocation(pr.Repository.Owner.Login, pr.Repository.Name, pr.Number),
			Owner:     pr.Repository.Owner.Login,
			Repo:      pr.Repository.Name,
			Title:     pr.Title,
			Number:    pr.Number,
			Author:    pr.Author.Login,
			State:     string(pr.State),
			CreatedAt: pr.CreatedAt.Unix(),
			UpdatedAt: pr.UpdatedAt.Unix(),
		},
	}
}

func ConvertRelease(release githubcli.Release) ent.Release {
	return ent.Release{
		Name:         release.Name,
		TagName:      release.TagName,
		IsLatest:     release.IsLatest,
		IsPrerelease: release.IsPrerelease,
		CreatedAt:    release.CreatedAt.Unix(),
		PublishedAt:  release.PublishedAt.Unix(),
	}
}

func ConvertLabel(label githubcli.Label) ent.Label {
	return ent.Label{
		Name:        label.Name,
		Description: label.Description,
		CreatedAt:   label.CreatedAt.Unix(),
		UpdatedAt:   label.UpdatedAt.Unix(),
	}
}

func ConvertRepository(repo githubcli.Repository) ent.Repository {
	return ent.Repository{
		RepositoryInDB: ent.RepositoryInDB{
			ID:        0,
			Location:  ent.NewRepoLocation(repo.Owner.Login, repo.Name),
			Owner:     repo.Owner.Login,
			Name:      repo.Name,
			UpdatedAt: repo.UpdatedAt.Unix(),
			PushedAt:  repo.PushedAt.Unix(),
		},
	}
}
