package approval

import (
	"time"

	"github.com/tidbcloud/devops/models/base"
)

type ApprovalTask struct {
	base.Updatable

	Level           int        `gorm:"not null"`
	NodeName        string     `gorm:"type:VARCHAR(255);not null"`
	User            string     `gorm:"type:VARCHAR(255);not null"`
	StartAt         *time.Time `gorm:"type:TIMESTAMP"`
	EndAt           *time.Time `gorm:"type:TIMESTAMP"`
	Status          string     `gorm:"type:VARCHAR(255);not null"`
	NotifyMessageID *string    `gorm:"type:VARCHAR(255)"`
	NotifyLarkType  *string    `gorm:"type:VARCHAR(255)"`

	// association
	ApprovalID uint64 `gorm:"index:idx_approval_id;not null"`
}

func (ApprovalTask) TableName() string {
	return "approval_tasks"
}

type ApprovalCCTask struct {
	base.Base

	User string `gorm:"type:VARCHAR(255);not null"`

	// association
	ApprovalID uint64 `gorm:"index:idx_approval_id;not null"`
}

func (ApprovalCCTask) TableName() string {
	return "approval_cc_tasks"
}

type ApprovalAdminTask struct {
	base.Updatable

	User   string     `gorm:"type:VARCHAR(255);not null"`
	EndAt  *time.Time `gorm:"type:TIMESTAMP"`
	Status string     `gorm:"type:VARCHAR(255);not null"`

	// association
	ApprovalID uint64 `gorm:"index:idx_approval_id;not null"`
}

type ApprovalNode struct {
	base.Base

	Name   string `gorm:"type:VARCHAR(255);not null"`
	Level  int    `gorm:"not null"`
	Method string `gorm:"type:VARCHAR(255);not null"`
	Status string `gorm:"type:VARCHAR(255);not null"`

	// association
	ApprovalID uint64 `gorm:"index:idx_approval_id;not null"`
}

func (ApprovalNode) TableName() string {
	return "approval_nodes"
}

type Approval struct {
	base.Updatable

	Name          string     `gorm:"type:VARCHAR(255);not null"`
	Applicant     string     `gorm:"type:VARCHAR(255);not null"`
	StartAt       *time.Time `gorm:"type:TIMESTAMP"`
	EndAt         *time.Time `gorm:"type:TIMESTAMP"`
	Status        string     `gorm:"type:VARCHAR(255);not null"`
	DetailLink    string     `gorm:"type:VARCHAR(1024)"`
	Parameters    []byte     `gorm:"type:JSON"`
	Overview      []byte     `gorm:"type:JSON"`
	Reason        string     `gorm:"type:VARCHAR(2048)"`
	Configuration []byte     `gorm:"type:JSON"`
	FromTemplate  *string    `gorm:"type:VARCHAR(255)"`

	// association
	Nodes      []*ApprovalNode      `gorm:"foreignkey:ApprovalID"`
	Tasks      []*ApprovalTask      `gorm:"foreignkey:ApprovalID"`
	CCTasks    []*ApprovalCCTask    `gorm:"foreignkey:ApprovalID"`
	AdminTasks []*ApprovalAdminTask `gorm:"foreignkey:ApprovalID"`
}

func (Approval) TableName() string {
	return "approval_approvals"
}
