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

@description('The name of the Application Insights instance')
param appInsightsName string

@description('The name of the Log Analytics Workspace')
param logAnalyticsWorkspaceName string

@description('The name of the Grafana instance')
param grafanaName string = 'grafana-${uniqueString(resourceGroupId())}'

// Create Log Analytics Workspace
resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
  name: logAnalyticsWorkspaceName
  location: location
  properties: {
    sku: {
      name: 'PerGB2018'
    }
    retentionInDays: 30
    features: {
      enableLogAccessUsingOnlyResourcePermissions: true
    }
  }
}

// Create Application Insights
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: appInsightsName
  location: location
  kind: 'web'
  properties: {
    Application_Type: 'web'
    WorkspaceResourceId: logAnalyticsWorkspace.id
    IngestionMode: 'LogAnalytics'
    publicNetworkAccessForIngestion: 'Enabled'
    publicNetworkAccessForQuery: 'Enabled'
  }
}

// Create Grafana Instance - Standard SKU (lowest cost managed option)
resource grafana 'Microsoft.Dashboard/grafana@2023-09-01' = {
  name: grafanaName
  location: location
  sku: {
    name: 'Standard'
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    apiKey: 'Enabled'
    autoLogoutMinutes: 0
    azureMonitorWorkspaceIntegrations: [
      {
        azureMonitorWorkspaceResourceId: logAnalyticsWorkspace.id
      }
    ]
    grafanaIntegrations: {
      azureMonitorWorkspaceIntegrations: [
        {
          azureMonitorWorkspaceResourceId: logAnalyticsWorkspace.id
        }
      ]
    }
  }
}

// Assign Monitoring Data Reader role to Grafana for Log Analytics Workspace
resource grafanaLogAnalyticsRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  scope: logAnalyticsWorkspace
  name: guid(logAnalyticsWorkspace.id, grafana.id, 'b24988ac-6180-42a0-ab88-20f7382dd24c')
  properties: {
    roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c')
    principalId: grafana.identity.principalId
    principalType: 'ServicePrincipal'
  }
}

// Assign Monitoring Data Reader role to Grafana for Application Insights
resource grafanaAppInsightsRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  scope: applicationInsights
  name: guid(applicationInsights.id, grafana.id, 'b24988ac-6180-42a0-ab88-20f7382dd24c')
  properties: {
    roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c')
    principalId: grafana.identity.principalId
    principalType: 'ServicePrincipal'
  }
}

// Outputs
output appInsightsName string = applicationInsights.name
output instrumentationKey string = applicationInsights.properties.InstrumentationKey
output connectionString string = applicationInsights.properties.ConnectionString
output workspaceId string = logAnalyticsWorkspace.id
output workspaceName string = logAnalyticsWorkspace.name
output grafanaName string = grafana.name
output grafanaId string = grafana.id
output grafanaEndpoint string = grafana.properties.endpoint
