# Budget Module
# Creates Azure Consumption Budgets for cost management

variable "subscription_id" {
  description = "Azure subscription ID"
  type        = string
}

variable "budget_name" {
  description = "Name of the budget"
  type        = string
}

variable "amount" {
  description = "Budget amount"
  type        = number
}

variable "time_grain" {
  description = "Time grain (Monthly, Quarterly, Annually)"
  type        = string
  default     = "Monthly"
}

variable "start_date" {
  description = "Budget start date (ISO 8601 format)"
  type        = string
}

variable "end_date" {
  description = "Budget end date (ISO 8601 format)"
  type        = string
}

variable "notification_thresholds" {
  description = "List of notification thresholds (0-100)"
  type        = list(number)
  default     = [50, 80, 100]
}

variable "contact_emails" {
  description = "List of email addresses for notifications"
  type        = list(string)
  default     = []
}

variable "contact_roles" {
  description = "List of contact roles for notifications"
  type        = list(string)
  default     = ["Owner", "Contributor"]
}

# Consumption Budget
resource "azurerm_consumption_budget_subscription" "main" {
  name            = var.budget_name
  subscription_id = var.subscription_id
  
  amount     = var.amount
  time_grain = var.time_grain
  
  time_period {
    start_date = var.start_date
    end_date   = var.end_date
  }
  
  # Notifications
  dynamic "notification" {
    for_each = var.notification_thresholds
    
    content {
      enabled        = true
      threshold      = notification.value
      operator       = "GreaterThan"
      threshold_type = "Actual"
      
      contact_emails = var.contact_emails
      contact_roles  = var.contact_roles
    }
  }
}

# Outputs
output "budget_id" {
  value = azurerm_consumption_budget_subscription.main.id
}

output "budget_name" {
  value = azurerm_consumption_budget_subscription.main.name
}

