package storage

import (
	"errors"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"

	"sproutgate-backend/internal/models"
)

func (s *Store) SaveReset(record models.ResetPassword) error {
	row := dbResetFromModel(record)
	return s.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&row).Error
}

func (s *Store) GetReset(account string) (models.ResetPassword, bool, error) {
	var row DBResetPassword
	result := s.db.First(&row, "account = ?", account)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		return models.ResetPassword{}, false, nil
	}
	if result.Error != nil {
		return models.ResetPassword{}, false, result.Error
	}
	return row.toModel(), true, nil
}

func (s *Store) DeleteReset(account string) error {
	return s.db.Delete(&DBResetPassword{}, "account = ?", account).Error
}
