package entities

import (
	"time"

	"github.com/google/uuid"
)

// PartyKeyShare represents the server's key share
type PartyKeyShare struct {
	ID         uuid.UUID
	PartyID    string
	PartyIndex int
	SessionID  uuid.UUID // Keygen session ID
	ThresholdN int
	ThresholdT int
	ShareData  []byte // Encrypted tss-lib LocalPartySaveData
	PublicKey  []byte // Group public key
	CreatedAt  time.Time
	LastUsedAt *time.Time
}

// NewPartyKeyShare creates a new party key share
func NewPartyKeyShare(
	partyID string,
	partyIndex int,
	sessionID uuid.UUID,
	thresholdN, thresholdT int,
	shareData, publicKey []byte,
) *PartyKeyShare {
	return &PartyKeyShare{
		ID:         uuid.New(),
		PartyID:    partyID,
		PartyIndex: partyIndex,
		SessionID:  sessionID,
		ThresholdN: thresholdN,
		ThresholdT: thresholdT,
		ShareData:  shareData,
		PublicKey:  publicKey,
		CreatedAt:  time.Now().UTC(),
	}
}

// MarkUsed updates the last used timestamp
func (k *PartyKeyShare) MarkUsed() {
	now := time.Now().UTC()
	k.LastUsedAt = &now
}

// IsValid checks if the key share is valid
func (k *PartyKeyShare) IsValid() bool {
	return k.ID != uuid.Nil &&
		k.PartyID != "" &&
		len(k.ShareData) > 0 &&
		len(k.PublicKey) > 0
}
