{
  "$schema": "https://schema.management.azure.com/schemas/0.1.2-preview/CreateUIDefinition.MultiVm.json#",
  "handler": "Microsoft.Azure.CreateUIDef",
  "version": "0.1.2-preview",
  "parameters": {
    "config": {
      "isWizard": true,
      "basics": {
        "location": {
          "label": "Location",
          "resourceTypes": [
            "Microsoft.Cache/redisEnterprise",
            "Microsoft.Network/virtualNetworks"
          ]
        },
        "resourceGroup": {
          "allowExisting": true
        }
      }
    },
    "basics": [],
    "steps": [
      {
        "name": "clusterSettings",
        "label": "AKS Cluster",
        "elements": [
          {
            "name": "clusterInfo",
            "type": "Microsoft.Common.InfoBox",
            "visible": true,
            "options": {
              "text": "Select the existing AKS cluster where the Azure Voting App will be installed.",
              "style": "Info"
            }
          },
          {
            "name": "clusterLookupControl",
            "type": "Microsoft.Solutions.ArmApiControl",
            "request": {
              "method": "GET",
              "path": "[concat(subscription().id, '/providers/Microsoft.ContainerService/managedClusters?api-version=2023-10-01')]"
            }
          },
          {
            "name": "clusterName",
            "type": "Microsoft.Common.DropDown",
            "label": "AKS Cluster Name",
            "toolTip": "Select the existing AKS cluster.",
            "constraints": {
              "allowedValues": "[map(steps('clusterSettings').clusterLookupControl.value, (item) => parse(concat('{\"label\":\"', item.name, '\",\"value\":\"', item.name, '\"}')))]" ,
              "required": true
            }
          }
        ]
      },
      {
        "name": "redisSettings",
        "label": "Azure Managed Redis",
        "elements": [
          {
            "name": "redisInfo",
            "type": "Microsoft.Common.InfoBox",
            "visible": true,
            "options": {
              "text": "Configure Azure Managed Redis. Redis will be deployed with a private endpoint and public network access disabled.",
              "style": "Info"
            }
          },
          {
            "name": "redisName",
            "type": "Microsoft.Common.TextBox",
            "label": "Managed Redis Name",
            "defaultValue": "",
            "toolTip": "Name for the Azure Managed Redis instance. Must be globally unique.",
            "constraints": {
              "required": true,
              "regex": "^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$",
              "validationMessage": "Must be 2-63 characters, alphanumeric and hyphens, start and end with alphanumeric."
            }
          },
          {
            "name": "redisSku",
            "type": "Microsoft.Common.DropDown",
            "label": "Managed Redis SKU",
            "defaultValue": "Balanced_B0",
            "toolTip": "SKU for Azure Managed Redis.",
            "constraints": {
              "allowedValues": [
                { "label": "Balanced_B0", "value": "Balanced_B0" },
                { "label": "Balanced_B1", "value": "Balanced_B1" },
                { "label": "Balanced_B3", "value": "Balanced_B3" }
              ],
              "required": true
            }
          }
        ]
      },
      {
        "name": "networkSettings",
        "label": "Networking",
        "elements": [
          {
            "name": "networkInfo",
            "type": "Microsoft.Common.InfoBox",
            "visible": true,
            "options": {
              "text": "Configure the virtual network. The AKS subnet and a private endpoint subnet will be created automatically.",
              "style": "Info"
            }
          },
          {
            "name": "vnetName",
            "type": "Microsoft.Common.TextBox",
            "label": "Virtual Network Name",
            "defaultValue": "",
            "toolTip": "Name for the new virtual network.",
            "constraints": {
              "required": true,
              "regex": "^[a-zA-Z0-9][a-zA-Z0-9._-]{0,78}[a-zA-Z0-9_]$",
              "validationMessage": "Must be 2-80 characters, alphanumeric, hyphens, underscores, or periods."
            }
          },
          {
            "name": "vnetAddressPrefix",
            "type": "Microsoft.Common.TextBox",
            "label": "VNet Address Prefix",
            "defaultValue": "10.0.0.0/16",
            "toolTip": "Address space for the virtual network (CIDR notation).",
            "constraints": {
              "required": true,
              "regex": "^([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$",
              "validationMessage": "Must be a valid CIDR range, e.g. 10.0.0.0/16."
            }
          },
          {
            "name": "aksSubnetPrefix",
            "type": "Microsoft.Common.TextBox",
            "label": "AKS Subnet Prefix",
            "defaultValue": "10.0.0.0/22",
            "toolTip": "Address prefix for the AKS subnet (must be within the VNet address space).",
            "constraints": {
              "required": true,
              "regex": "^([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$",
              "validationMessage": "Must be a valid CIDR range, e.g. 10.0.0.0/22."
            }
          },
          {
            "name": "peSubnetPrefix",
            "type": "Microsoft.Common.TextBox",
            "label": "Private Endpoint Subnet Prefix",
            "defaultValue": "10.0.4.0/24",
            "toolTip": "Address prefix for the Redis private endpoint subnet.",
            "constraints": {
              "required": true,
              "regex": "^([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$",
              "validationMessage": "Must be a valid CIDR range, e.g. 10.0.4.0/24."
            }
          }
        ]
      },
      {
        "name": "appSettings",
        "label": "Application Settings",
        "elements": [
          {
            "name": "voteTitle",
            "type": "Microsoft.Common.TextBox",
            "label": "App Title",
            "defaultValue": "Azure Voting App",
            "toolTip": "Title displayed on the voting app.",
            "constraints": {
              "required": true,
              "regex": "^.{1,64}$",
              "validationMessage": "Title must be between 1 and 64 characters."
            }
          },
          {
            "name": "voteValue1",
            "type": "Microsoft.Common.TextBox",
            "label": "Option 1",
            "defaultValue": "Cats",
            "toolTip": "First voting option.",
            "constraints": {
              "required": true,
              "regex": "^.{1,32}$",
              "validationMessage": "Option must be between 1 and 32 characters."
            }
          },
          {
            "name": "voteValue2",
            "type": "Microsoft.Common.TextBox",
            "label": "Option 2",
            "defaultValue": "Dogs",
            "toolTip": "Second voting option.",
            "constraints": {
              "required": true,
              "regex": "^.{1,32}$",
              "validationMessage": "Option must be between 1 and 32 characters."
            }
          }
        ]
      }
    ],
    "outputs": {
      "location": "[location()]",
      "clusterName": "[steps('clusterSettings').clusterName]",
      "redisName": "[steps('redisSettings').redisName]",
      "redisSku": "[steps('redisSettings').redisSku]",
      "vnetName": "[steps('networkSettings').vnetName]",
      "vnetAddressPrefix": "[steps('networkSettings').vnetAddressPrefix]",
      "aksSubnetPrefix": "[steps('networkSettings').aksSubnetPrefix]",
      "peSubnetPrefix": "[steps('networkSettings').peSubnetPrefix]",
      "vote_title": "[steps('appSettings').voteTitle]",
      "vote_value1": "[steps('appSettings').voteValue1]",
      "vote_value2": "[steps('appSettings').voteValue2]"
    }
  }
}
