module "main_vpc" {
  source = "../../../modules/vpc/vpc_v2"

  name_prefix       = local.name_prefix
  cidr_second_octet = var.vpc["cidr_second_octet"]

  flow_logs = {
    gsirt = {
      log_destination      = "${data.aws_s3_bucket.security_logs.arn}/vpcflow/"
      log_destination_type = "s3"
      traffic_type         = "REJECT"
    }
  }

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "Networking",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_NTW"
    }
  )
}

module "main_public_subnets" {
  source = "../../../modules/vpc/subnet"

  name_prefix        = local.name_prefix
  subnets            = var.public_subnets
  vpc_id             = module.main_vpc.vpc_id
  is_public          = true
  cidr_second_octet  = var.vpc["cidr_second_octet"]
  availability_zones = local.availability_zones

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "Networking",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_NTW"
    }
  )
}

module "main_public_routing" {
  source = "../../../modules/vpc/routes"

  is_public   = true
  name_prefix = local.name_prefix
  vpc_id      = module.main_vpc.vpc_id
  vpc_cidr    = module.main_vpc.vpc_cidr
  igw_id      = module.main_vpc.vpc_igw
  subnets     = module.main_public_subnets.subnet_ids

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "Networking",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_NTW"
    }
  )
}

module "main_private_subnets" {
  source = "../../../modules/vpc/subnet"

  name_prefix        = local.name_prefix
  subnets            = var.private_subnets
  vpc_id             = module.main_vpc.vpc_id
  is_public          = false
  cidr_second_octet  = var.vpc["cidr_second_octet"]
  availability_zones = local.availability_zones

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "Networking",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_NTW"
    }
  )
}

module "main_nat_gateways" {
  source = "../../../modules/vpc/nat"

  name_prefix = local.name_prefix
  common_tags = local.common_tags
  # Not using NAT gateway redundancy in dev
  public_subnet_ids = [module.main_public_subnets.subnet_ids[1]]
}

# In some environments it's better to create multiple nat gateways, one per availability zone.
module "main_private_routing" {
  source = "../../../modules/vpc/routes"

  is_public      = false
  name_prefix    = local.name_prefix
  common_tags    = local.common_tags
  vpc_id         = module.main_vpc.vpc_id
  vpc_cidr       = module.main_vpc.vpc_cidr
  nat_gateway_id = module.main_nat_gateways.nats[0].id
  subnets        = module.main_private_subnets.subnet_ids
}

module "apollo-dev_delphi-dev" {
  source = "../../../modules/vpc/peering/one-to-one-external"

  vpc_requester_cidr  = "10.${var.vpc.cidr_second_octet}.0.0/16"
  vpc_accepter_cidr   = "10.10.0.0/16"
  connection_name     = "APL_DEV_TO_DLP_DEV"
  accepter_account_id = module.aws_accounts_list.accounts["gdb-delphi-dev"]

  providers = {
    aws.requester = aws
    aws.accepter  = aws.gdb-delphi-dev
  }
}

module "apollo-dev_delphi-stage" {
  source = "../../../modules/vpc/peering/one-to-one-external"

  vpc_requester_cidr  = "10.${var.vpc.cidr_second_octet}.0.0/16"
  vpc_accepter_cidr   = "10.32.0.0/16"
  connection_name     = "APL_DEV_TO_DLP_STAGE"
  accepter_account_id = module.aws_accounts_list.accounts["gdb-delphi-dev"]

  providers = {
    aws.requester = aws
    aws.accepter  = aws.gdb-delphi-dev
  }
}

module "gsirt_endpoint" {
  source = "../../../modules/vpc/endpoint_gateway"

  service           = module.gsirt_regional_endpoints.regional_endpoints_service_name[var.aws_region_id]
  vpc_endpoint_type = "Interface"
  subnet_ids        = module.main_private_subnets.subnet_ids
  vpc_id            = module.main_vpc.vpc_id
  cidr_second_octet = var.vpc["cidr_second_octet"]

  common_tags = merge(
    local.common_tags,
    {
      Name                     = format("%v-gsirt-endpoint", local.name_prefix)
      project                  = "Infrastructure",
      service                  = "Networking",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_NTW"
    }
  )
}

resource "aws_security_group_rule" "apollo_legacy_redis" {
  for_each = toset([
    "${module.aws_accounts_list.accounts["gdb-delphi-dev"]}/sg-0c4ec5bf17fc2cc72",
  ])
  from_port                = 6379
  protocol                 = "tcp"
  security_group_id        = module.main_sg_map.redis_id
  to_port                  = 6379
  type                     = "ingress"
  source_security_group_id = each.key
}

resource "aws_security_group_rule" "apollo_qa_ecs" {
  from_port                = 8000
  protocol                 = "tcp"
  security_group_id        = module.main_sg_map.ecs_id
  to_port                  = 8000
  type                     = "ingress"
  source_security_group_id = "${local.account_id}/sg-066fb93f0320f4469" // qa-apollo-sg-ecs
}

resource "aws_security_group_rule" "apollo_qa_ecs_to_es" {
  from_port                = 443
  protocol                 = "tcp"
  security_group_id        = module.main_sg_map.es_id
  to_port                  = 443
  type                     = "ingress"
  source_security_group_id = "${local.account_id}/sg-066fb93f0320f4469" // qa-apollo-sg-ecs
}

resource "aws_security_group_rule" "apollo_stage_ecs" {
  from_port                = 8000
  protocol                 = "tcp"
  security_group_id        = module.main_sg_map.ecs_id
  to_port                  = 8000
  type                     = "ingress"
  source_security_group_id = "${local.account_id}/sg-04579b0d29529247b" // stage-apollo-sg-ecs
}

resource "aws_security_group_rule" "apollo_stage_ecs_to_es" {
  from_port                = 443
  protocol                 = "tcp"
  security_group_id        = module.main_sg_map.es_id
  to_port                  = 443
  type                     = "ingress"
  source_security_group_id = "${local.account_id}/sg-04579b0d29529247b" // stage-apollo-sg-ecs
}

data "aws_ec2_transit_gateway" "transit_gateway" {
  filter {
    name   = "options.amazon-side-asn"
    values = ["64513"]
  }
}

data "aws_ec2_managed_prefix_list" "vpn_ny_users" {
  filter {
    name   = "prefix-list-name"
    values = ["vpn-ny-users"]
  }
}

resource "aws_ec2_transit_gateway_vpc_attachment" "transit_gateway_attachment" {
  vpc_id             = module.main_vpc.vpc_id
  transit_gateway_id = data.aws_ec2_transit_gateway.transit_gateway.id
  subnet_ids = concat(
    module.main_private_subnets.subnet_ids,
  )
}

resource "aws_route" "ny" {
  for_each = toset([module.main_private_routing.route_table_id, module.main_public_routing.route_table_id])

  route_table_id             = each.value
  destination_prefix_list_id = data.aws_ec2_managed_prefix_list.vpn_ny_users.id
  transit_gateway_id         = data.aws_ec2_transit_gateway.transit_gateway.id
}

