package auth

import (
	"time"

	"gorm.io/gorm"

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

type UserMicrosoftOAuthToken struct {
	ID                    []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	UserID                []byte     `gorm:"type:binary(16);uniqueIndex:ux_user_ms_oauth_user_provider;not null;column:user_id"`
	Provider              string     `gorm:"type:varchar(32);uniqueIndex:ux_user_ms_oauth_user_provider;not null;column:provider"`
	TenantID              string     `gorm:"type:varchar(128);index;not null;column:tenant_id"`
	MicrosoftObjectID     string     `gorm:"type:varchar(191);not null;column:microsoft_object_id"`
	MicrosoftSubject      string     `gorm:"type:varchar(191);not null;column:microsoft_subject"`
	MicrosoftSessionID    string     `gorm:"type:varchar(191);column:microsoft_session_id"`
	RefreshTokenEncrypted []byte     `gorm:"type:blob;not null;column:refresh_token_encrypted"`
	AccessTokenEncrypted  []byte     `gorm:"type:blob;column:access_token_encrypted"`
	IDTokenEncrypted      []byte     `gorm:"type:blob;column:id_token_encrypted"`
	Scope                 string     `gorm:"type:text;column:scope"`
	TokenType             string     `gorm:"type:varchar(32);column:token_type"`
	AccessTokenExpiresAt  *time.Time `gorm:"column:access_token_expires_at"`
	RefreshTokenExpiresAt *time.Time `gorm:"column:refresh_token_expires_at"`
	RefreshTokenRotatedAt *time.Time `gorm:"column:refresh_token_rotated_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 (UserMicrosoftOAuthToken) TableName() string { return "user_microsoft_oauth_tokens" }

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