{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "webAppName": {
            "type": "string",
            "defaultValue": "[concat('webApp-', uniqueString(resourceGroup().id))]",
            "metadata": {
                "description": "Web app name."
            },
            "minLength": 2
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "sku": {
            "type": "string",
            "defaultValue": "F1",
            "metadata": {
                "description": "The SKU of App Service Plan."
            }
        },
        "language": {
            "type": "string",
            "defaultValue": ".net",
            "allowedValues": [
                ".net",
                "php",
                "node",
                "html"
            ],
            "metadata": {
                "description": "The language stack of the app."
            }
        },    
        "repoUrl": {
            "type": "string",
            "defaultValue": "",
            "metadata": {
                "description": "Optional Git Repo URL, if empty a 'hello world' app will be deploy from the Azure-Samples repo"
            }
        }
    },
    "variables": {
        "appServicePlanPortalName": "[concat('AppServicePlan-', parameters('webAppName'))]",
        "gitRepoReference":{
            ".net":"https://github.com/Azure-Samples/app-service-web-dotnet-get-started",
            "node":"https://github.com/Azure-Samples/nodejs-docs-hello-world",
            "php":"https://github.com/Azure-Samples/php-docs-hello-world",
            "html":"https://github.com/Azure-Samples/html-docs-hello-world"
        },
        "gitRepoUrl":"[if(empty(parameters('repoUrl')), variables('gitRepoReference')[parameters('language')], parameters('repoUrl'))]",
        "configReference": {
            ".net": {
                "comments": ".Net app. No additional configuration needed."
            },
            "html": {
                "comments": "HTML app. No additional configuration needed."
            },
            "php": {
                "phpVersion": "7.4"
            },
            "node": {
                "appSettings": [
                    {
                        "name": "WEBSITE_NODE_DEFAULT_VERSION",
                        "value": "12.15.0"
                    }
                ]
            }
        }
    },
    "resources": [
        {
            "apiVersion": "2019-08-01",
            "type": "Microsoft.Web/serverfarms",
            "name": "[variables('appServicePlanPortalName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[parameters('sku')]"
            }
        },
        {
            "apiVersion": "2019-08-01",
            "type": "Microsoft.Web/sites",
            "name": "[parameters('webAppName')]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanPortalName'))]"
            ],
            "properties": {
                "siteConfig": "[variables('configReference')[parameters('language')]]",
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanPortalName'))]"
            },
            "resources": [
                {
                    "type": "sourcecontrols",
                    "apiVersion": "2019-08-01",
                    "name": "web",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/sites', parameters('webAppName'))]"
                    ],
                    "properties": {
                        "repoUrl": "[variables('gitRepoUrl')]",
                        "branch": "master",
                        "isManualIntegration": true
                    }
                }
            ]
        }
    ]
}
