{
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "siteName": {
            "type": "string"
        },
        "siteLocation": {
            "type": "string",
            "defaultValue": "West US"
        },
        "sku": {
            "type": "string",
            "allowedValues": [
                "Free",
                "Shared",
                "Basic",
                "Standard"
            ],
            "defaultValue": "Free"
        },
        "sqlServerName": {
            "type": "string"
        },
        "sqlAdministratorLogin": {
            "type": "string"
        },
        "sqlAdministratorPassword": {
            "type": "securestring"
        },
        "deployApplications": {
            "type": "string",
            "allowedValues": [
                "Manager and Storefront",
                "Manager Only"
            ],
            "defaultValue": "Manager and Storefront"
        },

        "repoUrl": {
            "type": "string"
        },
        "branch": {
            "type": "string"
        },
        "hostingPlanName": {
            "type": "string"
        },
        "workerSize": {
            "type": "string",
            "allowedValues": [
                "0",
                "1",
                "2"
            ],
            "defaultValue": "0"
        }
    },
    "variables": {
        "Manager Only": {
            "configValue": "Web Admin Only",
            "rootDir": "admin"
        },
        "Manager and Storefront": {
            "configValue": "Manager and Storefront",
            "rootDir": "store"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Sql/servers",
            "name": "[parameters('sqlServerName')]",
            "location": "[parameters('siteLocation')]",
            "apiVersion": "2.0",
            "properties": {
                "administratorLogin": "[parameters('sqlAdministratorLogin')]",
                "administratorLoginPassword": "[parameters('sqlAdministratorPassword')]"
            },
            "resources": [
                {
                    "type": "databases",
                    "name": "[parameters('siteName')]",
                    "location": "[parameters('siteLocation')]",
                    "apiVersion": "2.0",
                    "dependsOn": [
                        "[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]"
                    ],
                    "properties": {
                        "edition": "Basic",
                        "collation": "SQL_Latin1_General_CP1_CI_AS",
                        "maxSizeBytes": "1073741824",
                        "requestedServiceObjectiveId": "dd6d99bb-f193-4ec1-86f2-43d3bccbc49c"
                    }
                },
                {
                    "type": "firewallrules",
                    "name": "AllowAllWindowsAzureIps",
                    "location": "[parameters('siteLocation')]",
                    "apiVersion": "2.0",
                    "dependsOn": [
                        "[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]"
                    ],
                    "properties": {
                        "endIpAddress": "0.0.0.0",
                        "startIpAddress": "0.0.0.0"
                    }
                }
            ]
        },
        {
            "type": "Microsoft.Web/serverFarms",
            "name": "[parameters('hostingPlanName')]",
            "location": "[parameters('siteLocation')]",
            "apiVersion": "2014-06-01",
            "properties": {
                "name": "[parameters('hostingPlanName')]",
                "sku": "[parameters('sku')]",
                "workerSize": "[parameters('workerSize')]",
                "numberOfWorkers": 1
            }
        },
        {
            "type": "Microsoft.Web/Sites",
            "name": "[parameters('siteName')]",
            "location": "[parameters('siteLocation')]",
            "apiVersion": "2014-06-01",
            "dependsOn": [
                "[concat('Microsoft.Web/serverFarms/', parameters('hostingPlanName'))]"
            ],
            "tags": {
                "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "empty"
            },
            "properties": {
                "name": "[parameters('siteName')]",
                "serverFarm": "[parameters('hostingPlanName')]"
            },
            "resources": [
                {
                    "type": "config",
                    "name": "web",
                    "apiVersion": "2014-04-01",
                    "dependsOn": [
                        "[concat('Microsoft.Web/Sites/', parameters('siteName'))]"
                    ],
                    "properties": {
                        "connectionStrings": [
                            {
                                "Name": "VirtoCommerce",
                                "ConnectionString": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', parameters('sqlServerName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('siteName'), ';User Id=', parameters('sqlAdministratorLogin'), '@', parameters('sqlServerName'), ';Password=', parameters('sqlAdministratorPassword'), ';MultipleActiveResultSets=True;Connection Timeout=30;Trusted_Connection=False;Encrypt=True;')]",
                                "Type": 2
                            },
                            {
                                "Name": "VirtoCommerceBaseUrl",
                                "ConnectionString": "[concat('http://', parameters('siteName'), '.azurewebsites.net/admin')]",
                                "Type": 3
                            },
                            {
                                "Name": "AssetsConnectionString",
                                "ConnectionString": "[concat('provider=LocalStorage;rootPath=~/App_Data/Assets;publicUrl=http://', parameters('siteName'), '.azurewebsites.net/assets')]",
                                "Type": 3
                            }
                        ],
                        "appSettings": [
                            {
                                "name": "VirtoCommerce_DeployApplications",
                                "value": "[variables(parameters('deployApplications')).configValue]"
                            },
                            {
                                "name": "VirtoCommerce:Storefront.AppData.Path",
                                "value": "..\\store\\App_Data"
                            }
                        ],
                        "virtualApplications": [
                            {
                                "virtualPath": "/",
                                "physicalPath": "[concat('site\\wwwroot\\', variables(parameters('deployApplications')).rootDir)]",
                                "virtualDirectories": [
                                    {
                                        "virtualPath": "/assets",
                                        "physicalPath": "site\\wwwroot\\admin\\App_Data\\Assets"
                                    }
                                ]
                            },
                            {
                                "virtualPath": "/admin",
                                "physicalPath": "site\\wwwroot\\admin"
                            }
                        ]
                    }
                },
                {
                    "type": "sourcecontrols",
                    "name": "web",
                    "apiVersion": "2014-04-01",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/Sites', parameters('siteName'))]",
                        "[concat('Microsoft.Web/Sites/', parameters('siteName'), '/config/web')]"
                    ],
                    "properties": {
                        "RepoUrl": "[parameters('repoUrl')]",
                        "branch": "[parameters('branch')]",
                        "IsManualIntegration": true
                    }
                }
            ]
        }
    ]
}
