package server

import (
	"context"

	"github.com/google/uuid"

	"scrabble/backend/internal/payments"
	"scrabble/backend/internal/session"
)

// walletGate resolves the payments gate inputs for an account on the current request: the
// trusted execution context (the session platform carried on ctx by the platformContext
// middleware; absent ⇒ untrusted, fail-closed) and the account's present identity sources
// (which chip/benefit segments are awake, §6). The payments domain holds no cross-schema
// identity knowledge, so the server supplies present from account.Identities.
func (s *Server) walletGate(ctx context.Context, accountID uuid.UUID) (payments.Context, []payments.Source, error) {
	var cxt payments.Context
	if p, ok := session.PlatformFromContext(ctx); ok {
		cxt = payments.NewContext(p.Kind, p.Subtype)
	}
	ids, err := s.accounts.Identities(ctx, accountID)
	if err != nil {
		return payments.Context{}, nil, err
	}
	kinds := make([]string, len(ids))
	for i, id := range ids {
		kinds[i] = id.Kind
	}
	return cxt, payments.PresentSources(kinds), nil
}
