package core

import "github.com/pocketbase/pocketbase/tools/security"

// Email returns the "email" record field value (usually available with Auth collections).
func (m *Record) Email() string {
	return m.GetString(FieldNameEmail)
}

// SetEmail sets the "email" record field value (usually available with Auth collections).
func (m *Record) SetEmail(email string) {
	m.Set(FieldNameEmail, email)
}

// Verified returns the "emailVisibility" record field value (usually available with Auth collections).
func (m *Record) EmailVisibility() bool {
	return m.GetBool(FieldNameEmailVisibility)
}

// SetEmailVisibility sets the "emailVisibility" record field value (usually available with Auth collections).
func (m *Record) SetEmailVisibility(visible bool) {
	m.Set(FieldNameEmailVisibility, visible)
}

// Verified returns the "verified" record field value (usually available with Auth collections).
func (m *Record) Verified() bool {
	return m.GetBool(FieldNameVerified)
}

// SetVerified sets the "verified" record field value (usually available with Auth collections).
func (m *Record) SetVerified(verified bool) {
	m.Set(FieldNameVerified, verified)
}

// TokenKey returns the "tokenKey" record field value (usually available with Auth collections).
func (m *Record) TokenKey() string {
	return m.GetString(FieldNameTokenKey)
}

// SetTokenKey sets the "tokenKey" record field value (usually available with Auth collections).
func (m *Record) SetTokenKey(key string) {
	m.Set(FieldNameTokenKey, key)
}

// RefreshTokenKey generates and sets a new random auth record "tokenKey".
func (m *Record) RefreshTokenKey() {
	m.Set(FieldNameTokenKey+autogenerateModifier, "")
}

// SetPassword sets the "password" record field value (usually available with Auth collections).
func (m *Record) SetPassword(password string) {
	// note: the tokenKey will be auto changed if necessary before db write
	m.Set(FieldNamePassword, password)
}

// SetRandomPassword sets the "password" auth record field to a random autogenerated value.
//
// The autogenerated password is ~30 characters and it is set directly as hash,
// aka. the field plain password value validators (length, pattern, etc.) are ignored
// (this is usually used as part of the auto created OTP or OAuth2 user flows).
func (m *Record) SetRandomPassword() string {
	pass := security.RandomString(30)

	m.Set(FieldNamePassword, pass)
	m.RefreshTokenKey() // manually refresh the token key because the plain password is resetted

	// unset the plain value to skip the field validators
	if raw, ok := m.GetRaw(FieldNamePassword).(*PasswordFieldValue); ok {
		raw.Plain = ""
	}

	return pass
}

// ValidatePassword validates a plain password against the "password" record field.
//
// Returns false if the password is incorrect.
func (m *Record) ValidatePassword(password string) bool {
	pv, ok := m.GetRaw(FieldNamePassword).(*PasswordFieldValue)
	if !ok {
		return false
	}

	return pv.Validate(password)
}
