package storage

import (
	"errors"

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

	"sproutgate-backend/internal/models"
)

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

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

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

// DataDir 返回空字符串（MySQL 模式下无本地数据目录）。
func (s *Store) DataDir() string { return "" }
