{% set config = merged_configs.get('azurerm_backup_policy_vm', {}) %}
{% set comments = compliance_comments.get('azurerm_backup_policy_vm', []) %}
# ------------------------------------------------------------------------------
# Azure VM Backup Policy
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "azurerm_backup_policy_vm" "this" {
  name                = "{{ config.get('name', project_id ~ '-vm-backup-policy') }}"
  resource_group_name = var.resource_group_name
  recovery_vault_name = azurerm_recovery_services_vault.this.name

  backup {
    frequency = "{{ config.get('backup_frequency', 'Daily') }}"
    time      = "{{ config.get('backup_time', '23:00') }}"
  }

  retention_daily {
    count = {{ config.get('daily_retention_count', 30) }}
  }

  {% if config.get('weekly_retention_count') %}
  retention_weekly {
    count    = {{ config.weekly_retention_count }}
    weekdays = {{ config.get('weekly_retention_weekdays', '["Sunday"]') }}
  }
  {% endif %}

  {% if config.get('monthly_retention_count') %}
  retention_monthly {
    count    = {{ config.monthly_retention_count }}
    weekdays = {{ config.get('monthly_retention_weekdays', '["Sunday"]') }}
    weeks    = {{ config.get('monthly_retention_weeks', '["Last"]') }}
  }
  {% endif %}
}
