{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string",
            "metadata": {
                "description": "The name of you Virtual Machine."
            }
        },
        "adminUsername": {
            "type": "string",
            "metadata": {
                "description": "Username for the Virtual Machine."
            }
        },
        "publicKey": {
            "type": "securestring",
            "metadata": {
                "description": "SSH Key for the Virtual Machine"
            }
        },
        "imagePublisher": {
            "type": "string",
            "metadata": {
                "description": "The publisher of the VM image"
            },
	    "defaultValue": ""
        },
        "imageOffer": {
            "type": "string",
            "metadata": {
                "description": "The offer of the VM image"
            },
	    "defaultValue": ""
        },
        "imageSku": {
            "type": "string",
            "metadata": {
                "description": "The sku of the VM image"
            },
	    "defaultValue": ""
        },
        "imageVersion": {
            "type": "string",
            "metadata": {
                "description": "The version of the VM image"
            },
	    "defaultValue": ""
        },
	"imageId": {
	    "type": "string",
	    "metadata": {
		"description": "The resource id of the VM image. If provided, it will override the imagePublisher, imageOffer, imageSku and imageVersion parameters."
	    },
	    "defaultValue": ""
	},
        "vmSize": {
            "type": "string",
            "metadata": {
                "description": "The size of the VM"
            }
        },
        "vmTags": {
            "type": "object",
            "metadata": {
                "description": "Tags for the VM"
            }
        },
        "vmCount": {
            "type": "int",
            "metadata": {
                "description": "Number of VMs to deploy"
            }
        },
	"osDiskSize": {
	    "type": "int",
	    "defaultValue": 0,
	    "metadata": {
		"description": "Size of the OS disk in GB"
	    }
	},
        "provisionPublicIp": {
            "type": "bool",
            "defaultValue": true,
            "metadata": {
                "description": "If true creates a public ip"
            }
        },
        "priority": {
            "type": "string",
            "defaultValue": "Regular",
            "metadata": {
                "description": "Specifies the priority for the virtual machine."
            }
        },
        "evictionPolicy": {
            "type": "string",
            "defaultValue": "Delete",
            "metadata": {
                "description": "Specifies the eviction policy for the virtual machine."
            }
        },
        "billingProfile": {
            "type": "object",
            "defaultValue": {},
            "metadata": {
                "description": "Specifies the maximum price to pay for Azure Spot VM."
            }
        },
        "msi": {
            "type": "string",
            "metadata": {
                "description": "Managed service identity resource id."
            }
        },
        "nsg": {
            "type": "string",
            "metadata": {
                "description": "Network security group resource id."
            }
        },
        "subnet": {
            "type": "string",
            "metadata": {
                "descriptions": "Subnet resource id."
            }
        },
        "enableAcceleratedNetworking": {
            "type": "bool",
            "defaultValue": false,
            "metadata": {
                "descriptions": "Whether to enable accelerated networking."
            }
        },
        "zones": {
            "type": "array",
            "defaultValue": [],
            "metadata": {
                "description": "Availability zones for the virtual machine. If empty, no zones will be specified."
            }
        }
    },
    "variables": {
        "location": "[resourceGroup().location]",
        "useZones": "[greater(length(parameters('zones')), 0)]",
        "networkInterfaceNamePrivate": "[concat(parameters('vmName'), '-nic')]",
        "networkInterfaceNamePublic": "[concat(parameters('vmName'), '-nic-public')]",
        "networkInterfaceName": "[if(parameters('provisionPublicIp'), variables('networkInterfaceNamePublic'), variables('networkInterfaceNamePrivate'))]",
        "networkIpConfig": "[guid(resourceGroup().id, parameters('vmName'))]",
        "osDiskType": "Standard_LRS",
        "publicIpAddressName": "[concat(parameters('vmName'), '-ip')]"
    },
    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2024-07-01",
            "name": "[concat(variables('networkInterfaceNamePublic'), copyIndex())]",
            "location": "[variables('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/publicIpAddresses/', concat(variables('publicIpAddressName'), copyIndex()))]"
            ],
            "copy": {
                "name": "NICPublicCopy",
                "count": "[parameters('vmCount')]"
            },
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "[variables('networkIpConfig')]",
                        "properties": {
                            "subnet": {
                                "id": "[parameters('subnet')]"
                            },
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIpAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('publicIPAddressName'), copyIndex()))]"
                            }
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[parameters('nsg')]"
                },
                "enableAcceleratedNetworking": "[parameters('enableAcceleratedNetworking')]"
            },
            "condition": "[parameters('provisionPublicIp')]"
        },
        {
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2024-07-01",
            "name": "[concat(variables('networkInterfaceNamePrivate'), copyIndex())]",
            "location": "[variables('location')]",
            "copy": {
                "name": "NICPrivateCopy",
                "count": "[parameters('vmCount')]"
            },
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "[variables('networkIpConfig')]",
                        "properties": {
                            "subnet": {
                                "id": "[parameters('subnet')]"
                            },
                            "privateIPAllocationMethod": "Dynamic"
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[parameters('nsg')]"
                },
                "enableAcceleratedNetworking": "[parameters('enableAcceleratedNetworking')]"
            },
            "condition": "[not(parameters('provisionPublicIp'))]"
        },
        {
            "type": "Microsoft.Network/publicIpAddresses",
            "apiVersion": "2024-07-01",
            "name": "[concat(variables('publicIpAddressName'), copyIndex())]",
            "location": "[variables('location')]",
            "properties": {
                "publicIpAllocationMethod": "Static",
                "publicIPAddressVersion": "IPv4"
            },
            "copy": {
                "name": "PublicIpCopy",
                "count": "[parameters('vmCount')]"
            },
            "zones": "[if(variables('useZones'), createArray(string(parameters('zones')[mod(copyIndex(), length(parameters('zones')))])), json('null'))]",
            "sku": {
                "name": "Standard",
                "tier": "Regional"
            },
            "condition": "[parameters('provisionPublicIp')]"
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2024-07-01",
            "name": "[concat(parameters('vmName'), copyIndex())]",
            "location": "[variables('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/networkInterfaces/', concat(variables('networkInterfaceName'), copyIndex()))]"
            ],
            "copy": {
                "name": "VmCopy",
                "count": "[parameters('vmCount')]"
            },
            "tags": "[parameters('vmTags')]",
            "zones": "[if(variables('useZones'), createArray(string(parameters('zones')[mod(copyIndex(), length(parameters('zones')))])), json('null'))]",
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "storageProfile": {
                    "osDisk": {
                        "createOption": "fromImage",
			"diskSizeGB": "[if(equals(parameters('osDiskSize'), 0), json('null'), parameters('osDiskSize'))]",
                        "managedDisk": {
                            "storageAccountType": "[variables('osDiskType')]"
                        }
                    },
                    "imageReference": "[if(equals(parameters('imageId'), ''), json(concat('{\"publisher\":\"', parameters('imagePublisher'), '\",\"offer\":\"', parameters('imageOffer'), '\",\"sku\":\"', parameters('imageSku'), '\",\"version\":\"', parameters('imageVersion'), '\"}')), json(concat('{\"id\":\"', parameters('imageId'), '\"}')))]"
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('networkInterfaceName'), copyIndex()))]"
                        }
                    ]
                },
                "osProfile": {
                    "computerName": "[concat(parameters('vmName'), copyIndex())]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('publicKey')]",
                    "linuxConfiguration": {
                        "disablePasswordAuthentication": true,
                        "ssh": {
                            "publicKeys": [
                                {
                                    "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
                                    "keyData": "[parameters('publicKey')]"
                                }
                            ]
                        }
                    }
                },
                "priority": "[parameters('priority')]",
                "evictionPolicy": "[if(equals(parameters('priority'), 'Spot'), parameters('evictionPolicy'), '')]",
                "billingProfile": "[parameters('billingProfile')]"
            },
            "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                    "[parameters('msi')]": {
                    }
                }
            }
        }
    ],
    "outputs": {
        "publicIp": {
            "type": "array",
            "copy": {
                "count": "[parameters('vmCount')]",
                "input": "[reference(concat(variables('publicIpAddressName'), copyIndex())).ipAddress]"
            },
            "condition": "[parameters('provisionPublicIp')]"
        },
        "privateIp": {
            "type": "array",
            "copy": {
                "count": "[parameters('vmCount')]",
                "input": "[reference(concat(variables('networkInterfaceName'), copyIndex())).ipConfigurations[0].properties.privateIPAddress]"
            }
        }
    }
}
