// Package tencentim contains the server-side Tencent Cloud Chat integration boundary.
package tencentim

import (
	"time"

	"hyapp/pkg/tencentsig"
)

// UserSigConfig describes the stable inputs required by Tencent Cloud Chat UserSig.
type UserSigConfig struct {
	// SDKAppID is the application ID assigned by Tencent Cloud Chat.
	SDKAppID int64
	// SecretKey is the application key; it must stay on backend services only.
	SecretKey string
	// TTL controls how long the generated UserSig remains valid.
	TTL time.Duration
}

// UserSigResult is returned by gateway-service to authenticated Chat clients.
type UserSigResult struct {
	// SDKAppID lets clients initialize the Tencent Cloud Chat SDK.
	SDKAppID int64 `json:"sdk_app_id"`
	// UserID is the Tencent Chat identifier; this project uses decimal user_id.
	UserID string `json:"user_id"`
	// UserSig is the signed credential passed to Tencent Cloud Chat SDK login.
	UserSig string `json:"user_sig"`
	// ExpireAtMS lets clients refresh the credential before Tencent rejects login.
	ExpireAtMS int64 `json:"expire_at_ms"`
}

// GenerateUserSig signs a Tencent Cloud Chat UserSig through the shared Tencent signer.
func GenerateUserSig(cfg UserSigConfig, userID string, now time.Time) (UserSigResult, error) {
	result, err := tencentsig.GenerateUserSig(tencentsig.Config{
		SDKAppID:  cfg.SDKAppID,
		SecretKey: cfg.SecretKey,
		TTL:       cfg.TTL,
	}, userID, now)
	if err != nil {
		return UserSigResult{}, err
	}

	return UserSigResult{
		SDKAppID:   result.SDKAppID,
		UserID:     result.UserID,
		UserSig:    result.UserSig,
		ExpireAtMS: result.ExpireAtMS,
	}, nil
}
