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

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "qa-supply-chain-terraform-state"
    key     = "vpc/us_east_1_vpc/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_vpcs" "vpcs" {}

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

data "aws_ec2_managed_prefix_lists" "required_routes" {
  filter {
    name   = "prefix-list-name"
    values = var.vpc_routing_destination_prefix_lists
  }
}

data "aws_route53_resolver_firewall_rule_group" "dns_firewall_rule_group" {
  firewall_rule_group_id = "rslvr-frg-1764658298884585" # sony_ioc_rule_group
}

data "aws_route53profiles_profiles" "route53_profiles" {

}

module "networking-vpc" {
  source = "git@github.com:theorchard/terraform-vpc.git//modules/vpc?ref=4.2.0"

  env           = var.environment
  account_group = var.account_group
  vpc_cidr      = "10.225.0.0/20"

  vpc_public_subnets = [
    { availability_zone = "us-east-1a", cidr_block = "10.225.15.0/26", name = "${var.environment}_public_subnet_0" },
    { availability_zone = "us-east-1b", cidr_block = "10.225.15.64/26", name = "${var.environment}_public_subnet_1" },
    { availability_zone = "us-east-1c", cidr_block = "10.225.15.128/26", name = "${var.environment}_public_subnet_2" },
  ]

  vpc_private_subnets = [
    { availability_zone = "us-east-1a", cidr_block = "10.225.0.0/24", name = "${var.environment}_private_subnet_0" },
    { availability_zone = "us-east-1b", cidr_block = "10.225.1.0/24", name = "${var.environment}_private_subnet_1" },
    { availability_zone = "us-east-1c", cidr_block = "10.225.2.0/24", name = "${var.environment}_private_subnet_2" },
  ]

  vpc_flow_logs = {
    gsirt = {
      log_destination      = "arn:aws:s3:::${var.vpc_flow_logs_bucket}"
      log_destination_type = "s3"
      traffic_type         = "REJECT"
    }
  }
}

resource "aws_ec2_transit_gateway_vpc_attachment" "transit_gateway_attachment" {
  vpc_id             = module.networking-vpc.vpc_id
  subnet_ids         = module.networking-vpc.private_subnet_ids
  transit_gateway_id = data.aws_ec2_transit_gateway.transit_gateway.id
}

# Add routes for Orchard private networks to all private route tables
resource "aws_route" "private_internal_routes" {
  for_each = {
    for internal_route in local.internal_routes : "${internal_route.route_table_id}.${internal_route.prefix_list_id}" => internal_route
  }

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

resource "aws_route53_resolver_firewall_rule_group_association" "vpc_association" {
  for_each               = toset(data.aws_vpcs.vpcs.ids)
  name                   = "${each.key}-dns-firewall-rule-group-association"
  firewall_rule_group_id = data.aws_route53_resolver_firewall_rule_group.dns_firewall_rule_group.id
  priority               = 101
  vpc_id                 = each.key
}

resource "aws_route53profiles_association" "route53_profile_vpc_associations" {
  for_each    = { for profile in data.aws_route53profiles_profiles.route53_profiles.profiles : profile.name => profile if profile.name == var.qaorch_route53_profile }
  name        = each.key
  profile_id  = each.value.id
  resource_id = module.networking-vpc.vpc_id
}
