targetScope = 'subscription'

@description('The name of the resource group')
param resourceGroupName string = 'rg-blazor-grafana'

@description('The location for all resources')
param location string = 'eastus'

@description('The name of the App Service Plan')
param appServicePlanName string = 'asp-blazor-app'

@description('The name of the Web App')
param webAppName string = 'webapp-blazor-${uniqueString(resourceGroupName)}'

@description('The name of the Application Insights instance')
param appInsightsName string = 'ai-blazor-app'

@description('The name of the Log Analytics Workspace')
param logAnalyticsWorkspaceName string = 'law-blazor-app'

// Create the resource group
resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: resourceGroupName
  location: location
}

// Deploy the logging and monitoring infrastructure
module monitoring 'modules/monitoring.bicep' = {
  scope: resourceGroup
  name: 'monitoring-deployment'
  params: {
    location: location
    appInsightsName: appInsightsName
    logAnalyticsWorkspaceName: logAnalyticsWorkspaceName
  }
}

// Deploy the App Service infrastructure
module appService 'modules/appService.bicep' = {
  scope: resourceGroup
  name: 'appservice-deployment'
  params: {
    location: location
    appServicePlanName: appServicePlanName
    webAppName: webAppName
    appInsightsInstrumentationKey: monitoring.outputs.instrumentationKey
    appInsightsConnectionString: monitoring.outputs.connectionString
  }
}

// Outputs
output resourceGroupName string = resourceGroup.name
output webAppName string = appService.outputs.webAppName
output webAppUrl string = appService.outputs.webAppUrl
output appInsightsName string = monitoring.outputs.appInsightsName
output logAnalyticsWorkspaceId string = monitoring.outputs.workspaceId
output grafanaName string = monitoring.outputs.grafanaName
output grafanaEndpoint string = monitoring.outputs.grafanaEndpoint
