package Models

import (
	"database/sql/driver"
	"errors"
)

// MessageExpiry holds values for how long messages should expire by default
type MessageExpiry []uint8

const (
	// MessageExpiryFifteenMin expires after 15 minutes
	MessageExpiryFifteenMin = "fifteen_min"
	// MessageExpiryThirtyMin expires after 30 minutes
	MessageExpiryThirtyMin = "thirty_min"
	// MessageExpiryOneHour expires after one hour
	MessageExpiryOneHour = "one_hour"
	// MessageExpiryThreeHour expires after three hours
	MessageExpiryThreeHour = "three_hour"
	// MessageExpirySixHour expires after six hours
	MessageExpirySixHour = "six_hour"
	// MessageExpiryTwelveHour expires after twelve hours
	MessageExpiryTwelveHour = "twelve_hour"
	// MessageExpiryOneDay expires after one day
	MessageExpiryOneDay = "one_day"
	// MessageExpiryThreeDay expires after three days
	MessageExpiryThreeDay = "three_day"
	// MessageExpiryNoExpiry never expires
	MessageExpiryNoExpiry = "no_expiry"
)

// MessageExpiryValues list of all expiry values for validation
var MessageExpiryValues = []string{
	MessageExpiryFifteenMin,
	MessageExpiryThirtyMin,
	MessageExpiryOneHour,
	MessageExpiryThreeHour,
	MessageExpirySixHour,
	MessageExpiryTwelveHour,
	MessageExpiryOneDay,
	MessageExpiryThreeDay,
	MessageExpiryNoExpiry,
}

// Scan new value into MessageExpiry
func (e *MessageExpiry) Scan(value interface{}) error {
	var (
		strValue = value.(string)
		m        string
	)

	for _, m = range MessageExpiryValues {
		if strValue != m {
			continue
		}
		*e = MessageExpiry(strValue)
		return nil
	}

	return errors.New("Invalid MessageExpiry value")
}

// Value gets value out of MessageExpiry column
func (e MessageExpiry) Value() (driver.Value, error) {
	return string(e), nil
}

func (e MessageExpiry) String() string {
	return string(e)
}
