{% set config = merged_configs.get('aws_cloudwatch_event_rule', {}) %}
{% set comments = compliance_comments.get('aws_cloudwatch_event_rule', []) %}
# ------------------------------------------------------------------------------
# EventBridge Rule
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

{% if config.get('event_bus_name') %}
resource "aws_cloudwatch_event_bus" "this" {
  name = "{{ config.get('event_bus_name', project_id ~ '-bus') }}"

  tags = {
    Name      = "{{ config.get('event_bus_name', project_id ~ '-bus') }}"
    ManagedBy = "complyform"
  }
}
{% endif %}

resource "aws_cloudwatch_event_rule" "this" {
  name        = "{{ config.get('name', project_id ~ '-rule') }}"
  description = "{{ config.get('description', 'Managed by ComplyForm') }}"

  {% if config.get('event_bus_name') %}
  event_bus_name = aws_cloudwatch_event_bus.this.name
  {% endif %}

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

  {% if config.get('event_pattern') %}
  event_pattern = jsonencode({{ config.event_pattern }})
  {% endif %}

  state = "{{ config.get('state', 'ENABLED') }}"

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