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 = "us-east-1"

  default_tags {
    tags = module.default_tags.tags
  }
}

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

data "aws_s3_bucket" "flow_logs_bucket" {
  bucket = "${var.environment}-accounting-vpc-logs"
}

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" {}

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

data "aws_ec2_managed_prefix_list" "managed_prefix_list_routes" {
  for_each = toset(var.managed_prefix_lists_for_private_routes)
  filter {
    name   = "prefix-list-name"
    values = [each.value]
  }
}

module "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.214.0.0/20"
  dns_firewall_rule_group_id = data.aws_route53_resolver_firewall_rule_group.dns_firewall_rule_group.id


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

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

  vpc_flow_logs = {
    gsirt = {
      log_destination      = data.aws_s3_bucket.flow_logs_bucket.arn
      log_destination_type = "s3"
      traffic_type         = "REJECT"
    }
  }
}

resource "aws_route" "private_subnet_prefix_list_routes" {
  for_each = local.private_subnet_managed_prefix_list_routes

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

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

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 == "Route53-Z0317998NAZYP2141JBV-profile" }
  name        = each.key
  profile_id  = each.value.id
  resource_id = module.vpc.vpc_id
}
