package git

import "time"

type GitProvider string

const (
	GitHub GitProvider = "github"
)

var MigrateTables = []interface{}{
	&TiDBGitConnection{},
	&GithubAppTask{},
}

type TiDBGitConnection struct {
	ID        uint64    `gorm:"primaryKey;column:id;type:bigint(20);autoIncrement"`
	CreatedAt time.Time `gorm:"not null"`
	UpdatedAt time.Time `gorm:"not null"`

	GitProvider   GitProvider `gorm:"type:ENUM('github');not null;uniqueIndex:uk_git_repo"`
	RepoName      string      `gorm:"not null;column:repo_name;type:varchar(255);uniqueIndex:uk_git_repo"`
	TiDBProjectID uint64      `gorm:"not null;column:tidb_project_id;type:bigint unsigned;uniqueIndex:uk_tidb_cluster"`
	TiDBClusterID uint64      `gorm:"not null;column:tidb_cluster_id;type:bigint unsigned;uniqueIndex:uk_tidb_cluster"`
	TiDBOrgID     uint64      `gorm:"column:tidb_org_id;not null"`
}

func (TiDBGitConnection) TableName() string {
	return "tidb_git_connections"
}

type TaskStatus string

const (
	Pending   TaskStatus = "pending"
	Completed TaskStatus = "completed"
	Timeout   TaskStatus = "timeout"
	Dropped   TaskStatus = "dropped"
)

type GithubAppTask struct {
	ID                       uint64     `gorm:"primaryKey;column:id;type:bigint(20);autoIncrement"`
	TiDBProjectID            uint64     `gorm:"not null;column:tidb_project_id;type:bigint unsigned"`
	TiDBClusterID            uint64     `gorm:"not null;column:tidb_cluster_id;type:bigint unsigned"`
	TiDBBranchID             string     `gorm:"not null;column:tidb_branch_id;type:varchar(255)"`
	TiDBBranchName           string     `gorm:"not null;column:tidb_branch_name;type:varchar(255)"`
	GitHubCommentUUID        string     `gorm:"not null;column:github_comment_uuid;type:varchar(255)"`
	InstallationID           int64      `gorm:"not null;column:installation_id;type:bigint(20)"`
	GitHubOwner              string     `gorm:"not null;column:github_owner;type:varchar(255)"`
	GitHubRepo               string     `gorm:"not null;column:github_repo;type:varchar(255)"`
	GitHubPrNumber           int        `gorm:"not null;column:github_pr_number;type:int(11)"`
	GitHubCheckRunID         int64      `gorm:"not null;column:github_check_run_id;type:bigint(20)"`
	GitHubCommitID           string     `gorm:"not null;column:github_commit_id;type:varchar(255)"`
	TiDBBranchPreviousStatus string     `gorm:"not null;column:tidb_branch_previous_status;type:varchar(255)"`
	Status                   TaskStatus `gorm:"type:ENUM('pending','completed','timeout','dropped');not null"`
	Message                  string     `gorm:"not null;column:message;type:text"`
	CreatedAt                time.Time  `gorm:"not null;index"`
	UpdatedAt                time.Time  `gorm:"not null"`
	Action                   string     `gorm:"not null;column:action;type:varchar(255);default:''"`
	Before                   string     `gorm:"not null;column:before;type:varchar(255);default:''"`
	TiDBOrgID                uint64     `gorm:"column:tidb_org_id;not null"`
}

func (GithubAppTask) TableName() string {
	return "github_app_task"
}

type TaskStatusCount struct {
	Status TaskStatus
	Count  int
}
