{% set config = merged_configs.get('azurerm_cosmosdb_account', {}) %}
{% set comments = compliance_comments.get('azurerm_cosmosdb_account', []) %}
# ------------------------------------------------------------------------------
# Azure Cosmos DB Account
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "azurerm_cosmosdb_account" "this" {
  name                = "{{ config.get('name', project_id ~ '-cosmos') }}"
  resource_group_name = var.resource_group_name
  location            = var.region
  offer_type          = "Standard"

  kind = "{{ config.get('kind', 'GlobalDocumentDB') }}"

  # Consistency
  consistency_policy {
    consistency_level = "{{ config.get('consistency_level', 'Session') }}"
  }

  # Geo-location
  geo_location {
    location          = var.region
    failover_priority = 0
  }

  # TLS
  minimal_tls_version = "{{ config.get('minimal_tls_version', 'Tls12') }}"

  # Network
  public_network_access_enabled     = {{ config.get('public_network_access_enabled', false) | lower }}
  is_virtual_network_filter_enabled = {{ config.get('is_virtual_network_filter_enabled', true) | lower }}

  # Disable local auth (key-based) — use Azure AD
  local_authentication_disabled = {{ config.get('local_authentication_disabled', true) | lower }}

  # Automatic failover
  automatic_failover_enabled = {{ config.get('automatic_failover_enabled', true) | lower }}

  # Backup
  backup {
    type                = "{{ config.get('backup_type', 'Continuous') }}"
    tier                = "{{ config.get('backup_tier', 'Continuous30Days') }}"
  }

  {% if config.get('key_vault_key_id') %}
  key_vault_key_id = "{{ config.key_vault_key_id }}"
  {% endif %}

  {% if config.get('virtual_network_rule') %}
  {% for rule in config.virtual_network_rule %}
  virtual_network_rule {
    id = "{{ rule }}"
  }
  {% endfor %}
  {% endif %}

  tags = {
    Name      = "{{ config.get('name', project_id ~ '-cosmos') }}"
    ManagedBy = "complyform"
  }
}
