package entity

import (
	"github.com/jinzhu/gorm"
	"time"
)

// Az represents availability zone of IDC
type Az struct {
	gorm.Model
	Name     string `gorm:"type:varchar(256);unique_index"`
	Machines []Machine
}

type MachineStatus string

func (ms MachineStatus) String() string {
	return string(ms)
}

const (
	MachineOnline  MachineStatus = "online"
	MachineOffline MachineStatus = "offline"
)

type Machine struct {
	ID        string `gorm:"primary_key"`
	IP        string `gorm:"unique_index"`
	Hostname  string
	Status    string
	OS        string
	Kernel    string
	CPUCores  int32
	Memory    int64
	Rack      string
	NIC       string
	AzID      uint
	Purpose   string
	CreatedAt time.Time
}

// OperationType represents the type of operation event
type OperationType string
type ManagementType string

const (
	// UpdateOperation means that this is an update operation event
	UpdateOperation OperationType = "update"

	// DeleteOperation means that this is a delete operation event
	DeleteOperation OperationType = "delete"

	// CreateOperation means that this is a create operation event
	CreateOperation OperationType = "create"

	// OperateOperation means that this is an operate operation event
	OperateOperation OperationType = "operate"

	// AdminOperation means that this is an admin operation event
	AdminOperation OperationType = "admin"

	UserManagement     ManagementType = "user"
	PlatformManagement ManagementType = "platform"
	MachineManagement  ManagementType = "machine"
	InstanceManagement ManagementType = "instance"
)

// OperateEvent records the event of the platform operation
type OperateEvent struct {
	ID             uint   `gorm:"primary_key"`
	ManagementType string `sql:"index"`
	OperationType  string `sql:"index"`
	User           string
	OperationTime  int64  `sql:"index"`
	Content        string `gorm:"type:text"`
	Result         string
	Cost           int64
}

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(64);unique_index"`
	Password string
	Group    string `gorm:"type:varchar(1024);"`
}

type UserGroup struct {
	gorm.Model
	GroupName  string `gorm:"type:varchar(64);unique_index"`
	Privileges string `gorm:"type:varchar(1024);"`
}

type Alerts struct {
	ID           int64 `gorm:"primary_key"`
	Status       string
	StartsAt     time.Time
	EndsAt       time.Time
	Fingerprint  uint64
	GeneratorURL string
}

type AlertLabels struct {
	AlertID int64
	Label   string
	Value   string
}

type AlertAnnotations struct {
	AlertID    int64
	Annotation string
	Value      string
}

type Message struct {
	ID                uint   `gorm:"primary_key"`
	Type              string `sql:"index"`
	Time              int64  `sql:"index"`
	ContentTemplateID string
	TemplateArgs      string
}
