package mail

import "fmt"

func appendMessageAccountScope(
	baseQuery string,
	args []any,
	argIdx int,
	accountID string,
	scopedAccountIDs []string,
) (string, []any, int) {
	if accountID != "" {
		baseQuery += fmt.Sprintf(" AND m.account_id = $%d", argIdx)
		args = append(args, accountID)
		return baseQuery, args, argIdx + 1
	}
	if scopedAccountIDs == nil {
		return baseQuery, args, argIdx
	}
	if len(scopedAccountIDs) == 0 {
		return baseQuery + " AND FALSE", args, argIdx
	}
	baseQuery += fmt.Sprintf(" AND m.account_id = ANY($%d)", argIdx)
	args = append(args, scopedAccountIDs)
	return baseQuery, args, argIdx + 1
}
