package privilege

import (
	"time"

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

	"gorm.io/gorm"
)

type PrivilegeBinding struct {
	base.ID

	DeletedAt   gorm.DeletedAt
	User        string `gorm:"type:VARCHAR(255);index:idx_user_id"`
	PrivilegeID uint64 `gorm:"index:idx_privilege_id"`
}

func (PrivilegeBinding) TableName() string {
	return "privilege_bindings"
}

type Privilege struct {
	base.Updatable

	Type       string     `gorm:"type:VARCHAR(255);not null"`
	Source     string     `gorm:"type:VARCHAR(255)"`
	Content    []byte     `gorm:"type:JSON"`
	Reason     string     `gorm:"type:VARCHAR(2048);not null"`
	ExpiredAt  time.Time  `gorm:"type:TIMESTAMP"`
	ClosedAt   *time.Time `gorm:"type:TIMESTAMP"`
	Applicant  string     `gorm:"type:VARCHAR(255);not null"`
	Phase      string     `gorm:"type:VARCHAR(255);index:idx_phase;not null"`
	ApprovalID uint64     `gorm:"index:idx_approval_id"`

	Bindings []*PrivilegeBinding `gorm:"foreignkey:PrivilegeID"`
}

func (Privilege) TableName() string {
	return "privilege_privileges"
}
