package ultidraw

import (
	"context"
	"fmt"
	"strings"
)

func (s *Service) resolveCollabRoomID(ctx context.Context, ownerID, filePath string) (string, error) {
	ownerID = strings.TrimSpace(ownerID)
	filePath = normalizePath(filePath)
	if ownerID == "" || filePath == "" {
		return "", fmt.Errorf("collab room: missing owner or path")
	}
	if rev, err := s.nc.FileRevision(ctx, ownerID, filePath); err == nil && rev.FileID > 0 {
		return fmt.Sprintf("draw:%s:%d", ownerID, rev.FileID), nil
	}
	return fmt.Sprintf("draw:%s:%s", ownerID, hashPath(filePath)), nil
}

func (s *Service) ownerPathForPublic(ctx context.Context, token, password, clientCanonical, displayName string) (ownerID, ownerPath string, err error) {
	binding, err := s.nc.ResolvePublicShareBinding(ctx, token, password)
	if err != nil {
		return "", "", err
	}
	return binding.OwnerID, binding.OwnerPathForClient(clientCanonical, displayName), nil
}
