package services

import (
	"go-account-register/models"
	"go-account-register/utils"

	"gorm.io/gorm"
)

type EmailService struct {
	db *gorm.DB
}

func InitEmailService() *EmailService {
	db := utils.GetDB()

	return &EmailService{db: db}
}

func (e *EmailService) GetEmailOne() *models.Email {
	var email models.Email
	err := e.db.Model(email).Where("use_status = ? and status=?", 0, 1).Order("RAND()").Limit(1).Find(&email).Error
	if err != nil {
		return nil
	}
	return &email
}

func (e *EmailService) Update(email string, params models.Email) {
	e.db.Model(&models.Email{}).Where("email = ?", email).Updates(params)
}
