# Autoscaling for audio services
resource "aws_cloudwatch_metric_alarm" "sqs_audio_queue_visible_messages" {
  for_each = {
    for service in local.flattened_audio_service_list : "physical_location_${service.physical_location}-encoding_priority_${service.encoding_priority}" => service
  }
  alarm_name        = "SQS-${var.environment}-${var.service_name}-audio-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-visible-message-alarm"
  alarm_description = "SQS-${var.environment}-${var.service_name}-audio-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-visible-message-alarm"
  namespace         = "AWS/SQS"
  metric_name       = "ApproximateNumberOfMessagesVisible"
  statistic         = "Maximum"

  dimensions = {
    QueueName = "${var.environment}-delivery18_e000000${each.value.encoding_priority}_d0010000"
  }

  comparison_operator = "GreaterThanThreshold"
  threshold           = 0
  period              = 60
  evaluation_periods  = 1

  alarm_actions             = [aws_appautoscaling_policy.audio_scale_up_policy[each.key].arn]
  insufficient_data_actions = [aws_appautoscaling_policy.audio_scale_down_policy[each.key].arn]
  ok_actions                = [aws_appautoscaling_policy.audio_scale_down_policy[each.key].arn]
}


resource "aws_appautoscaling_policy" "audio_scale_up_policy" {
  for_each = {
    for service in local.flattened_audio_service_list : "physical_location_${service.physical_location}-encoding_priority_${service.encoding_priority}" => service
  }
  name               = "${var.environment}-${var.service_name}-audio-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-sqs-scale-up-policy"
  policy_type        = "StepScaling"
  service_namespace  = "ecs"
  resource_id        = "service/${module.vector_delivery_audio_ecs_service[each.key].fargate_cluster_name}/${module.vector_delivery_audio_ecs_service[each.key].fargate_worker_service_name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = var.scaling_cooldown_period_seconds
    metric_aggregation_type = "Maximum"

    step_adjustment {
      metric_interval_lower_bound = 0
      scaling_adjustment          = var.scale_up_adjustment
    }
  }
}

resource "aws_appautoscaling_policy" "audio_scale_down_policy" {
  for_each = {
    for service in local.flattened_audio_service_list : "physical_location_${service.physical_location}-encoding_priority_${service.encoding_priority}" => service
  }
  name               = "${var.environment}-${var.service_name}-audio-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-sqs-scale-down-policy"
  policy_type        = "StepScaling"
  service_namespace  = "ecs"
  resource_id        = "service/${module.vector_delivery_audio_ecs_service[each.key].fargate_cluster_name}/${module.vector_delivery_audio_ecs_service[each.key].fargate_worker_service_name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = var.scaling_cooldown_period_seconds
    metric_aggregation_type = "Maximum"

    step_adjustment {
      metric_interval_upper_bound = 0
      scaling_adjustment          = var.scale_down_adjustment
    }
  }
}

# Autoscaling for video services
resource "aws_cloudwatch_metric_alarm" "sqs_video_queue_visible_messages" {
  for_each = {
    for service in local.flattened_video_service_list : "physical_location_${service.physical_location}-encoding_priority_${service.encoding_priority}" => service
  }
  alarm_name        = "SQS-${var.environment}-${var.service_name}-video-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-visible-message-alarm"
  alarm_description = "SQS-${var.environment}-${var.service_name}-video-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-visible-message-alarm"
  namespace         = "AWS/SQS"
  metric_name       = "ApproximateNumberOfMessagesVisible"
  statistic         = "Maximum"

  dimensions = {
    QueueName = "${var.environment}-delivery17_e000000${each.value.encoding_priority}_d0010000"
  }

  comparison_operator = "GreaterThanThreshold"
  threshold           = 0
  period              = 60
  evaluation_periods  = 1

  alarm_actions             = [aws_appautoscaling_policy.video_scale_up_policy[each.key].arn]
  insufficient_data_actions = [aws_appautoscaling_policy.video_scale_down_policy[each.key].arn]
  ok_actions                = [aws_appautoscaling_policy.video_scale_down_policy[each.key].arn]
}


resource "aws_appautoscaling_policy" "video_scale_up_policy" {
  for_each = {
    for service in local.flattened_video_service_list : "physical_location_${service.physical_location}-encoding_priority_${service.encoding_priority}" => service
  }
  name               = "${var.environment}-${var.service_name}-video-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-sqs-scale-up-policy"
  policy_type        = "StepScaling"
  service_namespace  = "ecs"
  resource_id        = "service/${module.vector_delivery_video_ecs_service[each.key].fargate_cluster_name}/${module.vector_delivery_video_ecs_service[each.key].fargate_worker_service_name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = var.scaling_cooldown_period_seconds
    metric_aggregation_type = "Maximum"

    step_adjustment {
      metric_interval_lower_bound = 0
      scaling_adjustment          = var.scale_up_adjustment
    }
  }
}

resource "aws_appautoscaling_policy" "video_scale_down_policy" {
  for_each = {
    for service in local.flattened_video_service_list : "physical_location_${service.physical_location}-encoding_priority_${service.encoding_priority}" => service
  }
  name               = "${var.environment}-${var.service_name}-video-phys-loc-${each.value.physical_location}-p${each.value.encoding_priority}-sqs-scale-down-policy"
  policy_type        = "StepScaling"
  service_namespace  = "ecs"
  resource_id        = "service/${module.vector_delivery_video_ecs_service[each.key].fargate_cluster_name}/${module.vector_delivery_video_ecs_service[each.key].fargate_worker_service_name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = 30
    metric_aggregation_type = "Maximum"

    step_adjustment {
      metric_interval_upper_bound = 0
      scaling_adjustment          = var.scale_down_adjustment
    }
  }
}
