package authz

import (
	"bssapp-backend/auth"
	"context"
	"fmt"
)

func BuildMSSQLPiyasaFilter(
	ctx context.Context,
	column string,
) string {
	claims, ok := auth.GetClaimsFromContext(ctx)
	if ok && claims != nil && claims.IsAdmin() {
		return "1=1"
	}

	codes := GetPiyasaCodesFromCtx(ctx)

	if len(codes) == 0 {
		return "1=0"

	}
	return BuildMSSQLPiyasaFilterWithCodes(column, codes)
}

func BuildMSSQLPiyasaFilterWithCodes(column string, codes []string) string {
	normalizedCol := fmt.Sprintf("UPPER(LTRIM(RTRIM(%s)))", column)
	return BuildINClause(normalizedCol, codes)
}
