{
  "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
  "id": "multi-channel-notification",
  "name": "Multi-Channel Notification",
  "description": "Send notifications across multiple channels (Email, Teams, mobile push). Configurable channels and message templates.",
  "category": "notification",
  "tags": ["notification", "email", "teams", "alerts"],
  "parameters": {
    "recipients": {
      "type": "array",
      "description": "List of recipient email addresses",
      "required": true
    },
    "teamsChannel": {
      "type": "object",
      "description": "Teams channel info (teamId, channelId)",
      "required": false
    },
    "messageTitle": {
      "type": "string",
      "description": "Notification title",
      "required": true
    },
    "messageBody": {
      "type": "string",
      "description": "Notification body (HTML supported)",
      "required": true
    },
    "priority": {
      "type": "string",
      "description": "Message priority",
      "required": false,
      "default": "Normal",
      "enum": ["Low", "Normal", "High"]
    },
    "channels": {
      "type": "array",
      "description": "Channels to notify",
      "required": false,
      "default": ["email"],
      "enum": ["email", "teams", "push"]
    }
  },
  "connectionReferences": {
    "shared_office365": {
      "connectorId": "/providers/Microsoft.PowerApps/apis/shared_office365",
      "tier": "Standard",
      "description": "Office 365 Outlook for sending emails"
    },
    "shared_teams": {
      "connectorId": "/providers/Microsoft.PowerApps/apis/shared_teams",
      "tier": "Standard",
      "description": "Microsoft Teams for posting messages"
    }
  },
  "definition": {
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "contentVersion": "1.0.0.0",
    "triggers": {
      "manual": {
        "type": "Request",
        "kind": "Http",
        "inputs": {
          "schema": {
            "type": "object",
            "properties": {
              "title": { "type": "string" },
              "body": { "type": "string" },
              "recipients": {
                "type": "array",
                "items": { "type": "string" }
              },
              "priority": {
                "type": "string",
                "enum": ["Low", "Normal", "High"],
                "default": "Normal"
              },
              "channels": {
                "type": "array",
                "items": { "type": "string" },
                "default": ["email"]
              },
              "teamsTeamId": { "type": "string" },
              "teamsChannelId": { "type": "string" }
            },
            "required": ["title", "body", "recipients"]
          }
        }
      }
    },
    "actions": {
      "Initialize_results": {
        "type": "InitializeVariable",
        "inputs": {
          "variables": [{
            "name": "notificationResults",
            "type": "Array",
            "value": []
          }]
        },
        "runAfter": {}
      },
      "Check_email_channel": {
        "type": "If",
        "expression": {
          "contains": [
            "@coalesce(triggerBody()?['channels'], createArray('email'))",
            "email"
          ]
        },
        "actions": {
          "Send_emails": {
            "type": "Foreach",
            "foreach": "@triggerBody()?['recipients']",
            "actions": {
              "Send_email": {
                "type": "OpenApiConnection",
                "inputs": {
                  "host": {
                    "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
                    "connectionName": "shared_office365",
                    "operationId": "SendEmailV2"
                  },
                  "parameters": {
                    "emailMessage/To": "@{items('Send_emails')}",
                    "emailMessage/Subject": "@{triggerBody()?['title']}",
                    "emailMessage/Body": "@{triggerBody()?['body']}",
                    "emailMessage/Importance": "@{coalesce(triggerBody()?['priority'], 'Normal')}",
                    "emailMessage/IsHtml": true
                  }
                },
                "runAfter": {}
              },
              "Append_email_result": {
                "type": "AppendToArrayVariable",
                "inputs": {
                  "name": "notificationResults",
                  "value": {
                    "channel": "email",
                    "recipient": "@{items('Send_emails')}",
                    "status": "sent"
                  }
                },
                "runAfter": {
                  "Send_email": ["Succeeded"]
                }
              }
            },
            "runAfter": {}
          }
        },
        "else": {
          "actions": {}
        },
        "runAfter": {
          "Initialize_results": ["Succeeded"]
        }
      },
      "Check_teams_channel": {
        "type": "If",
        "expression": {
          "and": [
            {
              "contains": [
                "@coalesce(triggerBody()?['channels'], createArray('email'))",
                "teams"
              ]
            },
            {
              "not": {
                "equals": ["@triggerBody()?['teamsTeamId']", null]
              }
            }
          ]
        },
        "actions": {
          "Post_to_Teams": {
            "type": "OpenApiConnection",
            "inputs": {
              "host": {
                "apiId": "/providers/Microsoft.PowerApps/apis/shared_teams",
                "connectionName": "shared_teams",
                "operationId": "PostMessageToConversation"
              },
              "parameters": {
                "groupId": "@triggerBody()?['teamsTeamId']",
                "channelId": "@triggerBody()?['teamsChannelId']",
                "body/content": "<h3>@{triggerBody()?['title']}</h3>@{triggerBody()?['body']}",
                "body/contentType": "html"
              }
            },
            "runAfter": {}
          },
          "Append_teams_result": {
            "type": "AppendToArrayVariable",
            "inputs": {
              "name": "notificationResults",
              "value": {
                "channel": "teams",
                "teamId": "@{triggerBody()?['teamsTeamId']}",
                "status": "posted"
              }
            },
            "runAfter": {
              "Post_to_Teams": ["Succeeded"]
            }
          }
        },
        "else": {
          "actions": {}
        },
        "runAfter": {
          "Check_email_channel": ["Succeeded"]
        }
      },
      "Response": {
        "type": "Response",
        "kind": "Http",
        "inputs": {
          "statusCode": 200,
          "body": {
            "success": true,
            "results": "@variables('notificationResults')"
          }
        },
        "runAfter": {
          "Check_teams_channel": ["Succeeded"]
        }
      }
    }
  }
}
