package jump

import (
	"time"

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

type Jump struct {
	ID              base.ID `gorm:"primary_key;not null"`
	DestinationType string  `gorm:"type:VARCHAR(255);not null"`
	DestinationID   string  `gorm:"type:VARCHAR(255);not null;index:idx_destination_id"` // used by search
	Destination     []byte  `gorm:"type:JSON;not null"`
	Method          string  `gorm:"type:VARCHAR(255);not null"`
	Attributes      []byte  `gorm:"type:JSON"`

	// Associations
	AuthID uint64 `gorm:"index:idx_auth_id;not null"`
}

func (Jump) TableName() string {
	return "jump_jumps"
}

type Authorization struct {
	base.Updatable

	Privilege           string `gorm:"type:VARCHAR(255);not null"`
	Expiration          string `gorm:"type:VARCHAR(255);not null"`
	Reason              string `gorm:"type:VARCHAR(1024)"`
	Applicant           string `gorm:"type:VARCHAR(255);not null"` // email
	EnableGatewayAccess bool   `gorm:"not null;default:false"`

	// status
	Phase      string     `gorm:"type:VARCHAR(255);not null"`
	ApprovalID *uint64    `gorm:"index:idx_approval_id"`
	ClosedAt   *time.Time `gorm:"type:TIMESTAMP"`
	ExpireAt   *time.Time `gorm:"type:TIMESTAMP"`
	// Deprecated, replaced by Jump
	TokenExpireAt *time.Time `gorm:"type:TIMESTAMP"`

	// Associations
	Jumps []*Jump `gorm:"foreignKey:AuthID"`
}

func (Authorization) TableName() string {
	return "jump_authorizations"
}
