{
  "meta": {
    "$_GET": [
      {
        "type": "text",
        "name": "sort"
      },
      {
        "type": "text",
        "name": "dir"
      },
      {
        "type": "text",
        "name": "so_id"
      }
    ],
    "$_POST": [
      {
        "type": "text",
        "name": "calorie_query"
      }
    ]
  },
  "exec": {
    "steps": [
      {
        "name": "",
        "module": "auth",
        "action": "restrict",
        "options": {
          "provider": "security",
          "permissions": [
            "Active"
          ]
        }
      },
      {
        "name": "items_consumed_today",
        "module": "dbconnector",
        "action": "select",
        "options": {
          "connection": "db",
          "sql": {
            "type": "SELECT",
            "columns": [
              {
                "table": "calories_tracking",
                "column": "id"
              },
              {
                "table": "calories_tracking",
                "column": "item_name"
              },
              {
                "table": "calories_tracking",
                "column": "calories"
              },
              {
                "table": "calories_tracking",
                "column": "protein_gm"
              },
              {
                "table": "calories_tracking",
                "column": "carbs_gm"
              },
              {
                "table": "calories_tracking",
                "column": "fat_gm"
              },
              {
                "table": "calories_tracking",
                "column": "quantity"
              }
            ],
            "params": [
              {
                "operator": "equal",
                "type": "expression",
                "name": ":P1",
                "value": "{{NOW.formatDate('yyyy-MM-dd')}}",
                "test": ""
              }
            ],
            "table": {
              "name": "calories_tracking"
            },
            "primary": "id",
            "joins": [],
            "wheres": {
              "condition": "AND",
              "rules": [
                {
                  "id": "calories_tracking.intake_date",
                  "field": "calories_tracking.intake_date",
                  "type": "date",
                  "operator": "equal",
                  "value": "{{NOW.formatDate('yyyy-MM-dd')}}",
                  "data": {
                    "table": "calories_tracking",
                    "column": "intake_date",
                    "type": "date",
                    "columnObj": {
                      "type": "date",
                      "primary": false,
                      "nullable": false,
                      "name": "intake_date"
                    }
                  },
                  "operation": "="
                }
              ],
              "conditional": null,
              "valid": true
            },
            "query": "select `id`, `item_name`, `calories`, `protein_gm`, `carbs_gm`, `fat_gm`, `quantity` from `calories_tracking` where `calories_tracking`.`intake_date` = ?"
          }
        },
        "meta": [
          {
            "type": "number",
            "name": "id"
          },
          {
            "type": "text",
            "name": "item_name"
          },
          {
            "type": "number",
            "name": "calories"
          },
          {
            "type": "number",
            "name": "protein_gm"
          },
          {
            "type": "number",
            "name": "carbs_gm"
          },
          {
            "type": "number",
            "name": "fat_gm"
          },
          {
            "type": "text",
            "name": "quantity"
          }
        ],
        "outputType": "array",
        "output": false
      },
      {
        "name": "weight_logs",
        "module": "dbconnector",
        "action": "paged",
        "options": {
          "connection": "db",
          "sql": {
            "type": "SELECT",
            "columns": [
              {
                "table": "health_weight_trackor",
                "column": "date"
              },
              {
                "table": "health_weight_trackor",
                "column": "weight"
              }
            ],
            "params": [],
            "table": {
              "name": "health_weight_trackor"
            },
            "joins": [],
            "orders": [
              {
                "table": "health_weight_trackor",
                "column": "date",
                "direction": "DESC"
              }
            ],
            "query": "select `date`, `weight` from `health_weight_trackor` order by `date` DESC limit ?",
            "limit": 15
          }
        },
        "output": false,
        "meta": [
          {
            "name": "offset",
            "type": "number"
          },
          {
            "name": "limit",
            "type": "number"
          },
          {
            "name": "total",
            "type": "number"
          },
          {
            "name": "page",
            "type": "object",
            "sub": [
              {
                "name": "offset",
                "type": "object",
                "sub": [
                  {
                    "name": "first",
                    "type": "number"
                  },
                  {
                    "name": "prev",
                    "type": "number"
                  },
                  {
                    "name": "next",
                    "type": "number"
                  },
                  {
                    "name": "last",
                    "type": "number"
                  }
                ]
              },
              {
                "name": "current",
                "type": "number"
              },
              {
                "name": "total",
                "type": "number"
              }
            ]
          },
          {
            "name": "data",
            "type": "array",
            "sub": [
              {
                "type": "date",
                "name": "date"
              },
              {
                "type": "number",
                "name": "weight"
              }
            ]
          }
        ],
        "outputType": "object",
        "type": "dbconnector_paged_select"
      },
      {
        "name": "items_consumed",
        "module": "runjs",
        "action": "run",
        "options": {
          "code": "function jsonToMarkdown(data, title = \"Report\", level = 2) {\n  let md = \"\";\n\n  if (level === 2) {\n    md += `## ${title}\\n\\n`;\n  }\n\n  if (Array.isArray(data)) {\n    if (!data.length) return \"_No data_\\n\\n\";\n\n    // Array of objects → table\n    if (typeof data[0] === \"object\" && data[0] !== null) {\n      const headers = Object.keys(data[0]);\n\n      md += `| ${headers.join(\" | \")} |\\n`;\n      md += `| ${headers.map(() => \"---\").join(\" | \")} |\\n`;\n\n      data.forEach(row => {\n        md += `| ${headers.map(h => row[h] ?? \"\").join(\" | \")} |\\n`;\n      });\n\n      md += \"\\n\";\n      return md;\n    }\n\n    // Array of primitives\n    return data.map(v => `- ${v}`).join(\"\\n\") + \"\\n\\n\";\n  }\n\n  if (typeof data === \"object\" && data !== null) {\n    for (const key in data) {\n      const value = data[key];\n      const label = key\n        .replace(/_/g, \" \")\n        .replace(/([a-z])([A-Z])/g, \"$1 $2\")\n        .replace(/\\b\\w/g, l => l.toUpperCase());\n\n      if (typeof value === \"object\") {\n        md += `${\"#\".repeat(level)} ${label}\\n\\n`;\n        md += jsonToMarkdown(value, \"\", level + 1);\n      } else {\n        md += `- **${label}:** ${value}\\n`;\n      }\n    }\n    md += \"\\n\";\n  }\n\n  return md;\n}\n\nconst md = jsonToMarkdown(dynamicJson, \"Items consumed today\");\nreturn md;",
          "data": {
            "dynamicJson": "{{items_consumed_today}}"
          },
          "show_errors": true,
          "throw_errors": true
        },
        "output": false
      },
      {
        "name": "weight_logs",
        "module": "runjs",
        "action": "run",
        "options": {
          "code": "function jsonToMarkdown(data, title = \"Report\", level = 2) {\n  let md = \"\";\n\n  if (level === 2) {\n    md += `## ${title}\\n\\n`;\n  }\n\n  if (Array.isArray(data)) {\n    if (!data.length) return \"_No data_\\n\\n\";\n\n    // Array of objects → table\n    if (typeof data[0] === \"object\" && data[0] !== null) {\n      const headers = Object.keys(data[0]);\n\n      md += `| ${headers.join(\" | \")} |\\n`;\n      md += `| ${headers.map(() => \"---\").join(\" | \")} |\\n`;\n\n      data.forEach(row => {\n        md += `| ${headers.map(h => row[h] ?? \"\").join(\" | \")} |\\n`;\n      });\n\n      md += \"\\n\";\n      return md;\n    }\n\n    // Array of primitives\n    return data.map(v => `- ${v}`).join(\"\\n\") + \"\\n\\n\";\n  }\n\n  if (typeof data === \"object\" && data !== null) {\n    for (const key in data) {\n      const value = data[key];\n      const label = key\n        .replace(/_/g, \" \")\n        .replace(/([a-z])([A-Z])/g, \"$1 $2\")\n        .replace(/\\b\\w/g, l => l.toUpperCase());\n\n      if (typeof value === \"object\") {\n        md += `${\"#\".repeat(level)} ${label}\\n\\n`;\n        md += jsonToMarkdown(value, \"\", level + 1);\n      } else {\n        md += `- **${label}:** ${value}\\n`;\n      }\n    }\n    md += \"\\n\";\n  }\n\n  return md;\n}\n\nconst md = jsonToMarkdown(dynamicJson, \"Weight Logs\");\nreturn md;",
          "data": {
            "dynamicJson": "{{weight_logs.data}}"
          },
          "show_errors": true,
          "throw_errors": true
        },
        "output": false,
        "collapsed": true
      },
      {
        "name": "raw_value",
        "module": "core",
        "action": "setvalue",
        "options": {
          "key": "raw_value",
          "value": "{{items_consumed+' '+weight_logs}}"
        },
        "meta": [],
        "outputType": "text",
        "output": false
      },
      {
        "name": "health_summary",
        "module": "gemini-ai",
        "action": "processWithGemini",
        "options": {
          "request_type": "meal_summary",
          "raw_text": "{{raw_value}}",
          "socket_id": "{{$_GET.so_id}}"
        },
        "output": true
      },
      {
        "name": "",
        "module": "core",
        "action": "condition",
        "options": {
          "if": "{{health_summary.rating>0}}",
          "then": {
            "steps": [
              {
                "name": "delete",
                "module": "dbupdater",
                "action": "delete",
                "options": {
                  "connection": "db",
                  "sql": {
                    "type": "delete",
                    "table": "health_summary",
                    "wheres": {
                      "condition": "AND",
                      "rules": [
                        {
                          "id": "date",
                          "field": "date",
                          "type": "date",
                          "operator": "equal",
                          "value": "{{NOW.formatDate('yyyy-MM-dd')}}",
                          "data": {
                            "column": "date"
                          },
                          "operation": "="
                        }
                      ],
                      "conditional": null,
                      "valid": true
                    },
                    "returning": "id",
                    "query": "delete from `health_summary` where `date` = ?",
                    "params": [
                      {
                        "operator": "equal",
                        "type": "expression",
                        "name": ":P1",
                        "value": "{{NOW.formatDate('yyyy-MM-dd')}}",
                        "test": ""
                      }
                    ]
                  }
                },
                "meta": [
                  {
                    "name": "affected",
                    "type": "number"
                  }
                ]
              },
              {
                "name": "insert",
                "module": "dbupdater",
                "action": "insert",
                "options": {
                  "connection": "db",
                  "sql": {
                    "type": "insert",
                    "values": [
                      {
                        "table": "health_summary",
                        "column": "summary",
                        "type": "json",
                        "value": "{{health_summary}}"
                      },
                      {
                        "table": "health_summary",
                        "column": "date",
                        "type": "date",
                        "value": "{{NOW.formatDate('yyyy-MM-dd')}}"
                      }
                    ],
                    "table": "health_summary",
                    "returning": "id",
                    "query": "insert into `health_summary` (`date`, `summary`) values (?, ?)",
                    "params": [
                      {
                        "name": ":P1",
                        "type": "expression",
                        "value": "{{health_summary}}",
                        "test": ""
                      },
                      {
                        "name": ":P2",
                        "type": "expression",
                        "value": "{{NOW.formatDate('yyyy-MM-dd')}}",
                        "test": ""
                      }
                    ]
                  }
                },
                "meta": [
                  {
                    "name": "identity",
                    "type": "text"
                  },
                  {
                    "name": "affected",
                    "type": "number"
                  }
                ]
              }
            ]
          }
        },
        "outputType": "boolean"
      }
    ]
  }
}