data "aws_iam_policy_document" "lambda_assets_encoding_route_policy" {
  # checkov:skip=CKV_AWS_111:Ensure IAM policies does not allow write access without constraints
  statement {
    effect = "Allow"
    actions = [
      "s3:ListAllMyBuckets"
    ]
    resources = [
      "*"
    ]
  }

  statement {
    effect = "Allow"
    actions = [
      "s3:ListBucket"
    ]
    resources = [
      data.aws_s3_bucket.orcd_raw_assets.arn
    ]
  }

  statement {
    effect = "Allow"
    actions = [
      "s3:GetObject",
      "s3:GetObjectAcl"
    ]
    resources = [
      "${data.aws_s3_bucket.orcd_raw_assets.arn}/*"
    ]
  }
}

# Create assets-encoding-route IAM policy
resource "aws_iam_policy" "lambda_assets_encoding_route_policy" {
  name   = "${var.environment}-${var.lambda_service_name}-encoding-route-policy"
  policy = data.aws_iam_policy_document.lambda_assets_encoding_route_policy.json
}

module "encoding_route_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git?ref=1.0.1"

  environment_name = var.environment
  service_name     = "${var.lambda_service_name}-encoding-route"
}

module "lambda_assets_encoding_route" {
  source = "git@github.com:theorchard/terraform-lambda.git//?ref=3.1.9"

  environment         = var.environment
  application_family  = var.application_family
  use_container_image = true
  lambda_name         = "${var.lambda_service_name}-encoding-route"
  lambda_description  = "Determines the media type of the given asset and branches to either the audio or image flow"


  lambda_function_environment_variables = {
    Environment                  = var.environment
    SENTRY_DSN                   = module.sentry_lambda_assets_encoding_route.sentry_key_dsn_public_output
    DD_API_KEY_SECRET_ARN        = data.aws_secretsmanager_secret.datadog_api.arn
    EXPECTED_BUCKET_OWNER        = data.aws_caller_identity.current.account_id
  }

  lambda_function_timeout                        = "300"
  lambda_function_memory_size                    = "512"
  vpc_enabled                                    = true
  vpc_id                                         = data.aws_vpc.main.id
  vpc_subnet_ids                                 = var.vpc_subnet_ids
  lambda_function_reserved_concurrent_executions = local.max_concurrency
  iam_managed_policy_attachments = [
    aws_iam_policy.lambda_assets_encoding_route_policy.arn,
    module.encoding_route_owsrequest.policy_arn_output
  ]
}

module "sentry_lambda_assets_encoding_route" {
  source = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"

  environment        = var.environment
  teams              = [var.environment]
  service_name       = "${var.lambda_service_name}-encoding-route-v2" # v2 to differentiate from old sentry project"
  application_family = var.application_family
}

module "datadog_lambda_assets_encoding_route" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.7"

  environment                       = var.environment
  service_name                      = "${var.lambda_service_name}-encoding-route"
  lambda_invocation_monitor_enabled = false
  lambda_error_monitor_enabled      = true
  notification_endpoints            = var.notification_endpoints
}
