# Backup Module for Azure
# Creates Recovery Services Vault for VM backups

# Recovery Services Vault
resource "azurerm_recovery_services_vault" "main" {
  name                = "${var.cluster_name}-backup-vault"
  location            = var.location
  resource_group_name = var.resource_group_name
  sku                 = "Standard"
  
  # Enable soft delete and retention
  soft_delete_enabled = true
  
  tags = merge(var.tags, {
    Purpose = "Backup"
  })
}

# Backup Policy for VMs
resource "azurerm_backup_policy_vm" "daily" {
  name                = "${var.cluster_name}-daily-backup-policy"
  resource_group_name = var.resource_group_name
  recovery_vault_name = azurerm_recovery_services_vault.main.name
  
  # Daily backup at 2 AM
  timezone = "UTC"
  
  backup {
    frequency = "Daily"
    time      = "02:00"
  }
  
  retention_daily {
    count = var.environment == "prod" ? 30 : 7
  }
  
  retention_weekly {
    count    = var.environment == "prod" ? 12 : 4
    weekdays = ["Sunday"]
  }
  
  retention_monthly {
    count    = var.environment == "prod" ? 12 : 3
    weekdays = ["Sunday"]
    weeks    = ["First"]
  }
  
  retention_yearly {
    count    = var.environment == "prod" ? 7 : 1
    weekdays = ["Sunday"]
    weeks    = ["First"]
    months   = ["January"]
  }
}

# Outputs are defined in outputs.tf

