using FrameworkDAL.DTO.EIPConversation;
using System.Text;
namespace FrameworkBLL.EIPConversation.EIPHandlers.EIPEngine.FlowEngine
{
///
/// Converts EIPStructuredDataDTO to a plain-text string for text-only channels
/// (WhatsApp, SMS, Telegram). Called by channel handlers when StructuredData is present.
///
public static class EIPTextFormatter
{
///
/// Produces a human-readable text representation of structured data.
/// TABLE: one record per block, field labels with values, records separated by ---
/// CARD: single record displayed as label: value pairs
/// LIST: numbered list using the first field value per row
///
public static string FormatAsText(EIPStructuredDataDTO data)
{
if (data.Rows.Count == 0)
return "(No data)";
var sb = new StringBuilder();
switch (data.Type.ToUpperInvariant())
{
case "LIST":
for (int i = 0; i < data.Rows.Count; i++)
{
var row = data.Rows[i];
var firstVal = data.Fields.Count > 0 && row.TryGetValue(data.Fields[0].Field, out var fv)
? fv : string.Join(", ", row.Values);
sb.AppendLine($"{i + 1}. {firstVal}");
}
break;
case "CARD":
AppendRecord(sb, data.Fields, data.Rows[0]);
break;
default: // TABLE
for (int i = 0; i < data.Rows.Count; i++)
{
if (i > 0) sb.AppendLine("---");
AppendRecord(sb, data.Fields, data.Rows[i]);
}
break;
}
if (data.IsTruncated)
sb.AppendLine($"(Showing {data.Rows.Count} of {data.TotalCount})");
return sb.ToString().TrimEnd();
}
private static void AppendRecord(
StringBuilder sb,
System.Collections.Generic.List fields,
System.Collections.Generic.Dictionary row)
{
foreach (var field in fields)
{
var val = row.TryGetValue(field.Field, out var v) ? v : string.Empty;
sb.AppendLine($"*{field.Label}*: {val}");
}
}
}
}