package auth

import (
	"time"

	"gorm.io/gorm"

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

// UserWebAuthnCredential stores a passkey credential bound to a user.
type UserWebAuthnCredential struct {
	ID             []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	UserID         []byte     `gorm:"type:binary(16);index;not null;column:user_id"`
	Name           string     `gorm:"type:varchar(100);column:name"`
	CredentialID   []byte     `gorm:"type:varbinary(512);uniqueIndex;not null;column:credential_id"`
	CredentialJSON []byte     `gorm:"type:mediumblob;not null;column:credential_json"`
	LastUsedAt     *time.Time `gorm:"column:last_used_at"`
	CreatedAt      time.Time
	UpdatedAt      time.Time
}

func (UserWebAuthnCredential) TableName() string { return "user_webauthn_credentials" }

func (c *UserWebAuthnCredential) BeforeCreate(tx *gorm.DB) error {
	if len(c.ID) == 0 {
		c.ID = uuidv7.MustBytes()
	}
	return nil
}
