package auth

import (
	"time"

	"gorm.io/gorm"

	"wucher/internal/shared/pkg/uuidv7"
)

type UserEmailLoginOTP struct {
	ID           []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	UserID       []byte     `gorm:"type:binary(16);index;not null;column:user_id"`
	OTPHash      []byte     `gorm:"type:binary(32);not null;column:otp_hash"`
	ExpiresAt    time.Time  `gorm:"index;not null;column:expires_at"`
	UsedAt       *time.Time `gorm:"index;column:used_at"`
	AttemptCount int        `gorm:"not null;default:0;column:attempt_count"`
	MaxAttempts  int        `gorm:"not null;default:5;column:max_attempts"`
	ResendCount  int        `gorm:"not null;default:1;column:resend_count"`
	LastSentAt   time.Time  `gorm:"index;not null;column:last_sent_at"`
	CreatedAt    time.Time
	CreatedBy    []byte `gorm:"type:binary(16);column:created_by"`
	UpdatedAt    time.Time
	UpdatedBy    []byte `gorm:"type:binary(16);column:updated_by"`
}

func (UserEmailLoginOTP) TableName() string { return "user_email_login_otps" }

func (o *UserEmailLoginOTP) BeforeCreate(_ *gorm.DB) error {
	if len(o.ID) == 0 {
		o.ID = uuidv7.MustBytes()
	}
	return nil
}
