# Resource Groups Module
# Creates resource groups according to Well-Architected Framework best practices

variable "environment" {
  description = "Environment (prod, dev, test, staging)"
  type        = string
}

variable "location" {
  description = "Azure region"
  type        = string
}

variable "project_name" {
  description = "Project name"
  type        = string
  default     = "defi-oracle-mainnet"
}

variable "tags" {
  description = "Tags to apply to resource groups"
  type        = map(string)
  default     = {}
}

# Common tags
locals {
  common_tags = merge(
    {
      Environment = var.environment
      Project     = var.project_name
      ChainID     = "138"
      ManagedBy   = "Terraform"
    },
    var.tags
  )
}

# Networking Resource Group (Long-lived)
resource "azurerm_resource_group" "network" {
  name     = "rg-${var.environment}-network-001"
  location = var.location
  
  tags = merge(local.common_tags, {
    Purpose  = "Networking"
    Lifecycle = "Long-lived"
  })
}

# Compute Resource Group (Long-lived)
resource "azurerm_resource_group" "compute" {
  name     = "rg-${var.environment}-compute-001"
  location = var.location
  
  tags = merge(local.common_tags, {
    Purpose   = "Compute"
    Lifecycle = "Long-lived"
  })
}

# Storage Resource Group (Long-lived)
resource "azurerm_resource_group" "storage" {
  name     = "rg-${var.environment}-storage-001"
  location = var.location
  
  tags = merge(local.common_tags, {
    Purpose   = "Storage"
    Lifecycle = "Long-lived"
  })
}

# Security Resource Group (Long-lived)
resource "azurerm_resource_group" "security" {
  name     = "rg-${var.environment}-security-001"
  location = var.location
  
  tags = merge(local.common_tags, {
    Purpose   = "Security"
    Lifecycle = "Long-lived"
  })
}

# Monitoring Resource Group (Long-lived)
resource "azurerm_resource_group" "monitoring" {
  name     = "rg-${var.environment}-monitoring-001"
  location = var.location
  
  tags = merge(local.common_tags, {
    Purpose   = "Monitoring"
    Lifecycle = "Long-lived"
  })
}

# Identity Resource Group (Long-lived)
resource "azurerm_resource_group" "identity" {
  name     = "rg-${var.environment}-identity-001"
  location = var.location
  
  tags = merge(local.common_tags, {
    Purpose   = "Identity"
    Lifecycle = "Long-lived"
  })
}

# Temporary Resource Group (Ephemeral)
resource "azurerm_resource_group" "temp" {
  name     = "rg-${var.environment}-temp-001"
  location = var.location
  
  tags = merge(local.common_tags, {
    Purpose   = "Temporary"
    Lifecycle = "Ephemeral"
  })
}

# Outputs
output "network_rg_name" {
  value = azurerm_resource_group.network.name
}

output "compute_rg_name" {
  value = azurerm_resource_group.compute.name
}

output "storage_rg_name" {
  value = azurerm_resource_group.storage.name
}

output "security_rg_name" {
  value = azurerm_resource_group.security.name
}

output "monitoring_rg_name" {
  value = azurerm_resource_group.monitoring.name
}

output "identity_rg_name" {
  value = azurerm_resource_group.identity.name
}

output "temp_rg_name" {
  value = azurerm_resource_group.temp.name
}

