package auth

import (
	"time"

	"gorm.io/gorm"

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

type UserIdentity struct {
	ID              []byte `gorm:"type:binary(16);primaryKey;column:id"`
	UserID          []byte `gorm:"type:binary(16);uniqueIndex:ux_user_identity_user;not null;column:user_id"`
	Provider        string `gorm:"type:varchar(32);uniqueIndex:ux_provider_subject;not null;column:provider"`
	ProviderSubject string `gorm:"type:varchar(191);uniqueIndex:ux_provider_subject;not null;column:provider_subject"`
	Email           string `gorm:"type:varchar(191);index;column:email"`
	Metadata        []byte `gorm:"type:json;column:metadata"`
	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 (UserIdentity) TableName() string { return "user_identities" }

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