{% for window in resources -%}
resource "aws_ssm_maintenance_window" "window_{{ window.name_sanitized }}" {
  name     = "{{ window.Name }}"
  schedule = "{{ window.Schedule }}"
  duration = {{ window.Duration }}
  cutoff   = {{ window.Cutoff }}

  {%- if window.get('Description') %}
  description = "{{ window.Description }}"
  {%- endif %}

  {%- if window.get('ScheduleTimezone') %}
  schedule_timezone = "{{ window.ScheduleTimezone }}"
  {%- endif %}

  {%- if window.get('ScheduleOffset') %}
  schedule_offset = {{ window.ScheduleOffset }}
  {%- endif %}

  {%- if window.get('StartDate') %}
  start_date = "{{ window.StartDate }}"
  {%- endif %}

  {%- if window.get('EndDate') %}
  end_date = "{{ window.EndDate }}"
  {%- endif %}

  allow_unassociated_targets = {{ window.AllowUnassociatedTargets | lower }}
  enabled = {{ window.Enabled | lower }}

  tags = {
    {%- for key, value in window.get('tags_formatted', {}).items() -%}
    "{{ key }}" = "{{ value }}"
    {%- endfor %}
    "HasTargets" = "{{ window.has_targets | lower }}"
    "HasTasks" = "{{ window.has_tasks | lower }}"
  }
}

{%- for target in window.get('targets_formatted', []) %}
# Maintenance window target for {{ target.Name or target.WindowTargetId }}
resource "aws_ssm_maintenance_window_target" "target_{{ window.name_sanitized }}_{{ loop.index }}" {
  window_id     = aws_ssm_maintenance_window.window_{{ window.name_sanitized }}.id
  resource_type = "{{ target.ResourceType }}"

  {%- if target.get('Name') %}
  name = "{{ target.Name }}"
  {%- endif %}

  {%- if target.get('Description') %}
  description = "{{ target.Description }}"
  {%- endif %}

  {%- if target.get('OwnerInformation') %}
  owner_information = "{{ target.OwnerInformation }}"
  {%- endif %}

  {%- for target_spec in target.get('Targets', []) %}
  targets {
    key    = "{{ target_spec.Key }}"
    values = [
      {%- for value in target_spec.Values -%}
      "{{ value }}",
      {%- endfor %}
    ]
  }
  {%- endfor %}
}
{%- endfor %}

{%- for task in window.get('tasks_formatted', []) %}
# Maintenance window task for {{ task.Name or task.WindowTaskId }}
resource "aws_ssm_maintenance_window_task" "task_{{ window.name_sanitized }}_{{ loop.index }}" {
  window_id    = aws_ssm_maintenance_window.window_{{ window.name_sanitized }}.id
  task_arn     = "{{ task.TaskArn }}"
  task_type    = "{{ task.Type }}"

  {%- if task.get('Name') %}
  name = "{{ task.Name }}"
  {%- endif %}

  {%- if task.get('Description') %}
  description = "{{ task.Description }}"
  {%- endif %}

  {%- if task.get('ServiceRoleArn') %}
  service_role_arn = "{{ task.ServiceRoleArn }}"
  {%- endif %}

  priority = {{ task.get('Priority', 1) }}

  {%- if task.get('MaxConcurrency') %}
  max_concurrency = "{{ task.MaxConcurrency }}"
  {%- endif %}

  {%- if task.get('MaxErrors') %}
  max_errors = "{{ task.MaxErrors }}"
  {%- endif %}

  {%- if task.get('CutoffBehavior') %}
  cutoff_behavior = "{{ task.CutoffBehavior }}"
  {%- endif %}

  {%- for target_spec in task.get('Targets', []) %}
  targets {
    key    = "{{ target_spec.Key }}"
    values = [
      {%- for value in target_spec.Values -%}
      "{{ value }}",
      {%- endfor %}
    ]
  }
  {%- endfor %}

  {%- if task.get('TaskParameters') %}
  task_invocation_parameters {
    {%- if task.Type == 'RUN_COMMAND' %}
    run_command_parameters {
      {%- for param_name, param_value in task.TaskParameters.items() %}
      parameter {
        name   = "{{ param_name }}"
        values = ["{{ param_value }}"]
      }
      {%- endfor %}
    }
    {%- elif task.Type == 'AUTOMATION' %}
    automation_parameters {
      {%- for param_name, param_value in task.TaskParameters.items() %}
      parameter {
        name   = "{{ param_name }}"
        values = ["{{ param_value }}"]
      }
      {%- endfor %}
    }
    {%- elif task.Type == 'STEP_FUNCTIONS' %}
    step_functions_parameters {
      {%- if task.TaskParameters.get('Input') %}
      input = "{{ task.TaskParameters.Input }}"
      {%- endif %}
      {%- if task.TaskParameters.get('Name') %}
      name = "{{ task.TaskParameters.Name }}"
      {%- endif %}
    }
    {%- elif task.Type == 'LAMBDA' %}
    lambda_parameters {
      {%- if task.TaskParameters.get('Payload') %}
      payload = "{{ task.TaskParameters.Payload }}"
      {%- endif %}
    }
    {%- endif %}
  }
  {%- endif %}
}
{%- endfor %}

{%- if not loop.last %}

{% endif %}
{%- endfor %}