module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/mcp-proxy/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git//?ref=3.2.2"

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

locals {
  # Matches the non-prod DNS record the fargate module creates for this service.
  proxy_origin = "https://${var.environment}-${var.service_name}.theorchard.io"
  # Matches the non-prod DNS record created by qa/apollo-mcp.
  mcp_upstream_host = "${var.environment}-apollo-mcp.theorchard.io"
  # Amazon-provided DNS server for awsvpc-mode Fargate tasks — same address in
  # every AWS VPC, not environment-specific. nginx needs it explicitly since
  # its proxy_pass targets are variables (runtime DNS lookups; it doesn't read
  # /etc/resolv.conf).
  resolver = "169.254.169.253"
}

# nginx front door for the PDEGO MCP: OAuth 2.1 facade to Auth0 (PRM + AS
# metadata served at this origin, /authorize + /token + static /register
# bridged to Auth0) and streaming /mcp reverse proxy to qa-apollo-mcp.
# Mirrors the qa-apollo-mcp Fargate setup; like it, the ALB is VPC-internal
# for the QA pilot (VPN cohort). Public exposure for the prod cohort is a
# separate step (internet-facing ALB in front, see qa/ows-users/proxy.tf
# for the precedent).
module "mcp_proxy_fargate" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.5.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                   = var.environment
  service_name                  = var.service_name
  application_family            = var.application_family
  # The shared qa-orcd-waf-block ACL blocks bodies containing http://localhost,
  # which every native OAuth client sends to /register as its loopback
  # redirect_uri (RFC 8252) — use the count-only WAF instead.
  blocking_waf_enabled          = false
  aws_region                    = var.aws_region
  commit_sha                    = "latest"
  container_port                = "8080"
  task_type                     = "web_service"
  health_check_path             = "/health"
  desired_task_count            = 2
  task_cpu                      = 512
  task_memory                   = 1024
  maximum_capacity              = 4
  minimum_capacity              = 2
  scaling_cpu_target_value      = 50
  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids

  # The nginx config is env-substituted at container start
  # (nginx/templates/default.conf.template in the mcp-proxy repo). The image
  # bakes in no defaults of its own — every var the template references must
  # be supplied here, except MCP_UPSTREAM_SCHEME: entrypoint.sh defaults that
  # one to https, which is correct for every real upstream, so it's omitted.
  # PROXY_ORIGIN is what the PRM / AS metadata / 401 challenge advertise —
  # it must be the origin clients connect to.
  environment_variables = [
    {
      Environment = var.environment
    },
    {
      PROXY_ORIGIN = local.proxy_origin
    },
    {
      AUTH0_DOMAIN = var.auth0_domain
    },
    {
      AUTH0_ORGANIZATION = var.auth0_organization
    },
    {
      MCP_UPSTREAM_HOST = local.mcp_upstream_host
    },
    {
      APOLLO_MCP_CLIENT_ID = var.apollo_mcp_client_id
    },
    {
      RESOLVER = local.resolver
    },
    {
      CLIENT_REDIRECT_URI = var.client_redirect_uri
    },
  ]
}

module "mcp_proxy_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.18.2"
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
}
