package v1

import (
	"database/sql"
	"time"

	"gorm.io/plugin/soft_delete"
)

type CostAlertRule struct {
	ID         uint64                `json:"id" gorm:"primary_key;AUTO_INCREMENT;column:id"`
	CreatedAt  time.Time             `json:"createdAt" gorm:"column:createdAt"`
	UpdatedAt  sql.NullTime          `json:"updatedAt" gorm:"column:updatedAt"`
	DeletedAt  sql.NullTime          `json:"deletedAt" gorm:"column:deletedAt"`
	IsDel      soft_delete.DeletedAt `json:"isDel" gorm:"column:isDel;softDelete:flag,DeletedAtField:DeletedAt"`
	ResourceID uint64                `json:"resourceID" gorm:"column:resourceID"`
	Resource   CloudResource         `json:"resource" gorm:"foreignkey:ResourceID;references:ID"`
	Type       int                   `json:"type" gorm:"column:type"`
	Threshold  float64               `json:"threshold" gorm:"column:threshold"`
	Receivers  string                `json:"receivers" gorm:"column:receivers"`
	Options    string                `json:"options" gorm:"column:options"`
	StartDate  sql.NullTime          `json:"startDate" gorm:"column:startDate"`
	EndDate    sql.NullTime          `json:"endDate" gorm:"column:endDate"`
}

func (ca *CostAlertRule) TableName() string {
	return "cost_alert_rule"
}

const (
	TypeMonthAbs = iota + 1
	TypeBaseMonthBudget
	TypeDailyAbs
	TypeBaseDailyAverage
)

type CostAlertCreateBody struct {
	ResourceMID string              `json:"resourceMID"`
	Type        int                 `json:"type"`
	Threshold   float64             `json:"threshold"`
	Receivers   []CostAlertReceiver `json:"receivers"`
	StartDate   *string             `json:"startDate"`
	EndDate     *string             `json:"endDate"`
	Options     *AlertOptions       `json:"options"`
}

type AlertOptions struct {
	AverageDays int `json:"averageDays"`
}

type CostAlertReceiver struct {
	LarkUserID string `json:"larkUserID,omitempty"`
	LarkChatID string `json:"larkChatID,omitempty"`
}

type CostAlertUpdateBody struct {
	ID        uint64              `json:"id"`
	Type      int                 `json:"type"`
	Threshold float64             `json:"threshold"`
	Receivers []CostAlertReceiver `json:"receivers"`
	StartDate *string             `json:"startDate"`
	EndDate   *string             `json:"endDate"`
	Options   *AlertOptions       `json:"options"`
}

type CostAlertListDataItem struct {
	ID        uint64           `json:"id"`
	Type      int              `json:"type"`
	Threshold float64          `json:"threshold"`
	Receivers []*AlertReceiver `json:"receivers"`
	Options   string           `json:"options"`
	StartDate string           `json:"startDate"`
	EndDate   string           `json:"endDate"`
}
