# Storage Module for Azure
# Creates storage accounts for backups and shared storage
# Variables are defined in variables.tf

# Storage account for backups
resource "azurerm_storage_account" "backups" {
  name                     = substr("${replace(lower(var.cluster_name), "-", "")}b${substr(var.environment, 0, 1)}${substr(md5(var.resource_group_name), 0, 6)}", 0, 24)
  resource_group_name      = var.resource_group_name
  location                 = var.location
  account_tier             = "Standard"
  account_replication_type = var.environment == "prod" ? "GRS" : "LRS"
  min_tls_version          = "TLS1_2"
  
  # Enable blob versioning
  blob_properties {
    versioning_enabled = true
    delete_retention_policy {
      days = var.environment == "prod" ? 90 : 30
    }
  }
  
  tags = merge(var.tags, {
    Purpose = "backups"
  })
}

# Container for chaindata backups
resource "azurerm_storage_container" "chaindata" {
  name                  = "chaindata"
  storage_account_name  = azurerm_storage_account.backups.name
  container_access_type = "private"
}

# Container for configuration backups
resource "azurerm_storage_container" "config" {
  name                  = "config"
  storage_account_name  = azurerm_storage_account.backups.name
  container_access_type = "private"
}

# Storage account for shared configuration (optional)
resource "azurerm_storage_account" "shared" {
  name                     = substr("${replace(lower(var.cluster_name), "-", "")}s${substr(var.environment, 0, 1)}${substr(md5(var.resource_group_name), 0, 6)}", 0, 24)
  resource_group_name      = var.resource_group_name
  location                 = var.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  min_tls_version          = "TLS1_2"
  
  tags = merge(var.tags, {
    Purpose = "shared"
  })
}

# File share for shared configuration
resource "azurerm_storage_share" "config" {
  name                 = "config"
  storage_account_name = azurerm_storage_account.shared.name
  quota                = 10
}

# Outputs are defined in outputs.tf

