
resource "aws_api_gateway_rest_api" "fan_response_rest_api" {
  name        = "${var.environment}-${var.service_name}"
  description = "Fan Response REST API Gateway"
  endpoint_configuration {
    types = ["EDGE"]
  }
  lifecycle {
    create_before_destroy = true
  }
  tags = {
    application_family = var.application_family
    environment        = var.environment
    service_name       = var.service_name
    terraformed        = true
  }
}

resource "aws_api_gateway_stage" "sme_fan_response_rest_api_stage" {

  deployment_id         = aws_api_gateway_deployment.fan_response_rest_api_deployment.id
  rest_api_id           = aws_api_gateway_rest_api.fan_response_rest_api.id
  stage_name            = var.environment
  client_certificate_id = aws_api_gateway_client_certificate.fan_response_rest_api_client_certificate.id

  access_log_settings {
    destination_arn = aws_cloudwatch_log_group.fan_response_rest_api_log_group.arn
    format = jsonencode({
      "requestId" : "$context.requestId",
      "ip" : "$context.identity.sourceIp",
      "requestTime" : "$context.requestTime",
      "httpMethod" : "$context.httpMethod",
      "resourcePath" : "$context.resourcePath",
      "vendorPath" : "$context.path",
      "status" : "$context.status",
      "protocol" : "$context.protocol",
      "authorizeResultStatus": "$context.authorize.status",
      "authorizerServiceStatus": "$context.authorizer.status",
      "authorizerLatency": "$context.authorizer.latency",
      "authorizerRequestId": "$context.authorizer.requestId",
      "responseLength" : "$context.responseLength",
      "responseLatency" : "$context.responseLatency",
      "functionResponseStatus": "$context.integration.status",
      "integrationErrorMessage" : "$context.integrationErrorMessage",
      "integrationError": "$context.integration.error",
      "integrationRequestId": "$context.integration.requestId",
      "errorMessage" : "$context.error.message"
    })
  }
}

data "aws_lambda_function" "datadog_lambda_function" {
  function_name = "DatadogLambdaFunction"
}

resource "aws_cloudwatch_log_subscription_filter" "datadog_lambda_log_filter_api" {
  name            = "${var.environment}-${var.service_name}-log-subscription-filter"
  log_group_name  = "/aws/apigateway/${var.environment}-${var.service_name}"
  filter_pattern  = ""
  destination_arn = data.aws_lambda_function.datadog_lambda_function.arn
  distribution    = "ByLogStream"
}


moved {
  from = aws_api_gateway_method_settings.example
  to   = aws_api_gateway_method_settings.smf_response_api_gateway_method_settings
}
resource "aws_api_gateway_method_settings" "smf_response_api_gateway_method_settings" {
  rest_api_id = aws_api_gateway_rest_api.fan_response_rest_api.id
  stage_name  = aws_api_gateway_stage.sme_fan_response_rest_api_stage.stage_name
  method_path = "*/*"

  settings {
    metrics_enabled = true
    logging_level   = "INFO"
  }
}

data "aws_acm_certificate" "tls_cert" {
  domain   = var.acm_certificate_domain
  statuses = ["ISSUED"]
}

data "aws_route53_zone" "route53_zone" {
  name = var.route53_zone_name
}

data "aws_route53_zone" "route53_zone_networking" {
  provider = aws.networking

  name = var.route53_zone_name
}

resource "aws_api_gateway_domain_name" "smf_rest_api_domain_name" {
  domain_name              = "${var.environment}-${var.service_name}.${data.aws_route53_zone.route53_zone.name}"
  regional_certificate_arn = data.aws_acm_certificate.tls_cert.arn
  security_policy          = "TLS_1_2"

  endpoint_configuration {
    types = ["REGIONAL"]
  }

  tags = local.tags
}

resource "aws_route53_record" "smf_rest_api_route53_record" {
  name    = aws_api_gateway_domain_name.smf_rest_api_domain_name.domain_name
  type    = "A"
  zone_id = data.aws_route53_zone.route53_zone.id

  alias {
    evaluate_target_health = true
    name                   = aws_api_gateway_domain_name.smf_rest_api_domain_name.regional_domain_name
    zone_id                = aws_api_gateway_domain_name.smf_rest_api_domain_name.regional_zone_id
  }
}

resource "aws_route53_record" "smf_rest_api_route53_record_networking" {
  provider = aws.networking

  name    = aws_api_gateway_domain_name.smf_rest_api_domain_name.domain_name
  type    = "A"
  zone_id = data.aws_route53_zone.route53_zone_networking.id

  alias {
    evaluate_target_health = true
    name                   = aws_api_gateway_domain_name.smf_rest_api_domain_name.regional_domain_name
    zone_id                = aws_api_gateway_domain_name.smf_rest_api_domain_name.regional_zone_id
  }
}

resource "aws_api_gateway_base_path_mapping" "smf_rest_api_path_mapping" {
  api_id      = aws_api_gateway_rest_api.fan_response_rest_api.id
  stage_name  = aws_api_gateway_stage.sme_fan_response_rest_api_stage.stage_name
  domain_name = aws_api_gateway_domain_name.smf_rest_api_domain_name.domain_name
}
