package Models

import (
	"github.com/gofrs/uuid"
	"gorm.io/gorm"
)

// BeforeUpdate prevents updating the username or email if it has not changed
// This stops a unique constraint error
func (u *User) BeforeUpdate(tx *gorm.DB) (err error) {
	if !tx.Statement.Changed("Username") {
		tx.Statement.Omit("Username")
	}
	if !tx.Statement.Changed("Email") {
		tx.Statement.Omit("Email")
	}
	return nil
}

// User holds user data
type User struct {
	Base
	Username             string        `gorm:"not null;unique"   json:"username"`
	Password             string        `gorm:"not null"          json:"password"`
	ConfirmPassword      string        `gorm:"-"                 json:"confirm_password"`
	Email                string        `                         json:"email"`
	AsymmetricPrivateKey string        `gorm:"not null"          json:"asymmetric_private_key"` // Stored encrypted
	AsymmetricPublicKey  string        `gorm:"not null"          json:"asymmetric_public_key"`
	SymmetricKey         string        `gorm:"not null"          json:"symmetric_key"` // Stored encrypted
	AttachmentID         *uuid.UUID    `                         json:"attachment_id"`
	Attachment           Attachment    `                         json:"attachment"`
	MessageExpiryDefault MessageExpiry `gorm:"default:no_expiry" json:"-" sql:"type:ENUM('fifteen_min', 'thirty_min', 'one_hour', 'three_hour', 'six_hour', 'twelve_hour', 'one_day', 'three_day', 'no_expiry')"` // Stored encrypted
}
