resource "aws_cloudwatch_event_rule" "manifest_json_rule" {
  name        = "${var.environment}-${var.bucket_name}-manifest-json-rule"
  description = "Matches s3 lambda-content-integration-tests upload for *.json (manifest) files"
  event_pattern = jsonencode({
    "source" : [
      "aws.s3"
    ],
    "detail-type" : [
      "AWS API Call via CloudTrail"
    ],
    "detail" : {
      "eventSource" : [
        "s3.amazonaws.com"
      ],
      "eventName" : [
        "PutObject",
      ],
      "requestParameters" : {
        "bucketName" : [
          "${var.environment}-${var.bucket_name}"
        ],
        "key" : [
          {
            "suffix" : "manifest.json"
          }
        ]
      }
    }
  })
  tags = local.tags
}
# Policy to allow Eventbridge to assume this role
data "aws_iam_policy_document" "allow_eventbridge_to_assume_role" {
  statement {
    effect = "Allow"
    principals {
      type        = "Service"
      identifiers = ["events.amazonaws.com"]
    }
    actions = ["sts:AssumeRole"]
  }
}

# Eventbridge assumes this role
resource "aws_iam_role" "eventbridge_role" {
  name               = "Eventbridge-${var.environment}-${var.service_name}"
  assume_role_policy = data.aws_iam_policy_document.allow_eventbridge_to_assume_role.json

  tags = local.tags
}

# Attach the trigger ECS task policy so Eventbridge can run the ECS task
resource "aws_iam_role_policy_attachment" "eventbridge_run_ecs_integration_tests" {
  role       = aws_iam_role.eventbridge_role.id
  policy_arn = aws_iam_policy.integration_test_trigger_ecs_task_policy.arn
}

data "aws_ecs_task_definition" "integration_test_task_definition" {
  task_definition = "${var.environment}-${var.service_name}"
}

data "aws_ecs_cluster" "integration_tests_ecs_cluster" {
  cluster_name = "${var.environment}-${var.service_name}"
}

data "aws_security_group" "integration_tests_task_sg" {
  name = "${var.environment}-${var.service_name}-task-security-group"
}

resource "aws_cloudwatch_event_target" "run_ecs_task_target" {
  target_id = "Eventbridge-run-${var.environment}-${var.service_name}"
  arn       = data.aws_ecs_cluster.integration_tests_ecs_cluster.arn
  rule      = aws_cloudwatch_event_rule.manifest_json_rule.name
  role_arn  = aws_iam_role.eventbridge_role.arn
  ecs_target {
    task_count          = 1
    task_definition_arn = data.aws_ecs_task_definition.integration_test_task_definition.arn_without_revision
    launch_type         = "FARGATE"
    network_configuration {
      subnets          = module.vpc_info.default_private_subnet_ids
      security_groups  = [data.aws_security_group.integration_tests_task_sg.id]
      assign_public_ip = false
    }
    tags = local.tags
  }
  input_transformer {
    input_paths = {
      bucket_name   = "$.detail.requestParameters.bucketName"
      manifest_file = "$.detail.requestParameters.key"
    }
    input_template = <<EOT
{
  "containerOverrides": [
    {
      "name": "${var.service_name}",
      "command": [
        "process",
        "--bucket-name",
        "<bucket_name>",
        "--manifest-file",
        "<manifest_file>"
      ]
    }
  ]
}
    EOT
  }
}
