package analytics

import (
	"fmt"

	pkgErrors "github.com/nexuscrm/backend/pkg/errors"
)

// validateFilters checks runtime filters against a whitelist of allowed keys
func (s *DashboardService) validateFilters(filters map[string]interface{}) error {
	// Whitelist of allowed global filter keys
	// This prevents arbitrary field injection or internal variable overwrites
	allowedKeys := map[string]bool{
		"startDate": true, // Common date range
		"endDate":   true,
		"record":    true, // Record context (e.g., Account ID)
		"ownerId":   true, // View as user
		"name":      true, // Name filtering (e.g. search)
	}

	for k := range filters {
		// Allow "record.*" for specific field context if needed, but strict for now
		if !allowedKeys[k] {
			return pkgErrors.NewValidationError("filter", fmt.Sprintf("disallowed filter key: %s", k))
		}
	}
	return nil
}
