package models

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

// RecoveryCode represents a recovery code for account recovery
type RecoveryCode struct {
	ID        uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id"`
	UserID    uuid.UUID      `gorm:"type:uuid;not null;index" json:"user_id"`
	Code      string         `gorm:"not null" json:"-"`
	IsUsed    bool           `gorm:"default:false" json:"is_used"`
	UsedAt    *time.Time     `json:"used_at"`
	ExpiresAt time.Time      `json:"expires_at"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`

	// Relations
	User User `gorm:"foreignKey:UserID" json:"-"`
}

// BeforeCreate hook to generate UUID if not set
func (r *RecoveryCode) BeforeCreate(tx *gorm.DB) error {
	if r.ID == uuid.Nil {
		r.ID = uuid.New()
	}
	return nil
}

// TableName returns the table name for the RecoveryCode model
func (RecoveryCode) TableName() string {
	return "recovery_codes"
}
