package auth

import (
	"time"

	"gorm.io/gorm"

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

type RolePermission struct {
	ID           []byte `gorm:"type:binary(16);primaryKey;column:id"`
	RoleID       []byte `gorm:"type:binary(16);index;uniqueIndex:ux_role_permission;not null;column:role_id"`
	PermissionID []byte `gorm:"type:binary(16);index;uniqueIndex:ux_role_permission;not null;column:permission_id"`
	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 (RolePermission) TableName() string { return "role_permissions" }

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