{
  "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
  "id": "approval-workflow",
  "name": "Approval Workflow",
  "description": "Standard approval workflow with request, review, and notification stages. Supports single or multi-stage approvals with escalation.",
  "category": "approval",
  "tags": ["approval", "workflow", "business-process"],
  "parameters": {
    "approverEmail": {
      "type": "string",
      "description": "Email address of the approver",
      "required": true
    },
    "requestorEmail": {
      "type": "string",
      "description": "Email address of the person requesting approval",
      "required": true
    },
    "itemTitle": {
      "type": "string",
      "description": "Title of the item being approved",
      "required": true
    },
    "itemDetails": {
      "type": "string",
      "description": "Details about the item",
      "required": false,
      "default": ""
    },
    "itemLink": {
      "type": "string",
      "description": "Link to the item",
      "required": false
    },
    "timeoutDays": {
      "type": "number",
      "description": "Days before approval request times out",
      "required": false,
      "default": 7
    }
  },
  "connectionReferences": {
    "shared_approvals": {
      "connectorId": "/providers/Microsoft.PowerApps/apis/shared_approvals",
      "tier": "Standard",
      "description": "Approvals connector for creating and managing approval requests"
    },
    "shared_office365": {
      "connectorId": "/providers/Microsoft.PowerApps/apis/shared_office365",
      "tier": "Standard",
      "description": "Office 365 Outlook for sending notification emails"
    }
  },
  "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": "Button",
        "inputs": {
          "schema": {
            "type": "object",
            "properties": {
              "title": { "type": "string", "title": "Item Title" },
              "details": { "type": "string", "title": "Item Details" },
              "requestor": { "type": "string", "title": "Requestor Email" },
              "approver": { "type": "string", "title": "Approver Email" },
              "itemLink": { "type": "string", "title": "Item Link" }
            },
            "required": ["title", "requestor", "approver"]
          }
        }
      }
    },
    "actions": {
      "Start_approval": {
        "type": "OpenApiConnectionWebhook",
        "inputs": {
          "host": {
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_approvals",
            "connectionName": "shared_approvals",
            "operationId": "StartAndWaitForAnApproval"
          },
          "parameters": {
            "approvalType": "Basic",
            "NotificationSettings/NotificationUrl": "@{triggerBody()?['itemLink']}"
          },
          "body": {
            "title": "@{triggerBody()?['title']}",
            "assignedTo": "@{triggerBody()?['approver']}",
            "details": "@{triggerBody()?['details']}",
            "requestor": "@{triggerBody()?['requestor']}",
            "enableNotifications": true,
            "enableReassignment": true
          }
        },
        "runAfter": {},
        "description": "Create and wait for approval response"
      },
      "Check_approval_response": {
        "type": "If",
        "expression": {
          "equals": [
            "@body('Start_approval')?['outcome']",
            "Approve"
          ]
        },
        "actions": {
          "Send_approval_notification": {
            "type": "OpenApiConnection",
            "inputs": {
              "host": {
                "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
                "connectionName": "shared_office365",
                "operationId": "SendEmailV2"
              },
              "parameters": {
                "emailMessage/To": "@{triggerBody()?['requestor']}",
                "emailMessage/Subject": "Approved: @{triggerBody()?['title']}",
                "emailMessage/Body": "<p>Your request <b>@{triggerBody()?['title']}</b> has been approved by @{triggerBody()?['approver']}.</p><p>Comments: @{body('Start_approval')?['comments']}</p>",
                "emailMessage/Importance": "Normal",
                "emailMessage/IsHtml": true
              }
            },
            "runAfter": {}
          }
        },
        "else": {
          "actions": {
            "Send_rejection_notification": {
              "type": "OpenApiConnection",
              "inputs": {
                "host": {
                  "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
                  "connectionName": "shared_office365",
                  "operationId": "SendEmailV2"
                },
                "parameters": {
                  "emailMessage/To": "@{triggerBody()?['requestor']}",
                  "emailMessage/Subject": "Rejected: @{triggerBody()?['title']}",
                  "emailMessage/Body": "<p>Your request <b>@{triggerBody()?['title']}</b> has been rejected by @{triggerBody()?['approver']}.</p><p>Reason: @{body('Start_approval')?['comments']}</p>",
                  "emailMessage/Importance": "Normal",
                  "emailMessage/IsHtml": true
                }
              },
              "runAfter": {}
            }
          }
        },
        "runAfter": {
          "Start_approval": ["Succeeded"]
        },
        "description": "Check if approved or rejected and send appropriate notification"
      }
    }
  }
}
