resource "azurerm_storage_account" "o11y" {
  name                            = local.storage_account_name
  resource_group_name             = var.storage_resource_group_name
  location                        = data.azurerm_resource_group.o11y_storage.location
  account_tier                    = "Standard"
  account_replication_type        = "ZRS"
  account_kind                    = "StorageV2"
  access_tier                     = "Hot"
  min_tls_version                 = "TLS1_2"
  https_traffic_only_enabled      = true
  public_network_access_enabled   = true
  allow_nested_items_to_be_public = false
  tags                            = local.tags

  blob_properties {
    delete_retention_policy {
      days = 7
    }

    container_delete_retention_policy {
      days = 7
    }
  }
}

resource "azurerm_storage_container" "o11y" {
  for_each = local.storage_containers

  name                  = each.value
  storage_account_id    = azurerm_storage_account.o11y.id
  container_access_type = "private"
}

resource "azurerm_storage_management_policy" "o11y" {
  storage_account_id = azurerm_storage_account.o11y.id

  rule {
    name    = "delete-loki-after-91-days"
    enabled = true

    filters {
      prefix_match = ["${local.loki_container}/fake"]
      blob_types   = ["blockBlob"]
    }

    actions {
      base_blob {
        delete_after_days_since_modification_greater_than = 91
      }
    }
  }
}
