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     = "prod/swf-yt-conflict-elasticsearch/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

  environment = var.environment
}

module "efs_volume" {
  source = "git@github.com:theorchard/terraform-efs.git//?ref=4.1.0"

  providers = {
    aws.dns = aws.networking
  }

  environment              = var.environment
  service_name             = var.service_name
  vpc_id                   = module.vpc_info.vpc_id
  subnet_ids               = module.vpc_info.default_private_subnet_ids
  owner_gid                = "1010"
  owner_uid                = "1010"
  access_point_permissions = "700"
  application_family       = var.application_family
}

data "aws_iam_policy" "open_search_rw_policy" {
  name = var.open_search_rw_policy
}

# This rule will allow the fargate security group to acess the EFS filesystem
resource "aws_security_group_rule" "allow_efs_sg_inbound" {
  from_port                = 2049
  to_port                  = 2049
  protocol                 = "tcp"
  security_group_id        = module.efs_volume.efs_security_group_id_output
  source_security_group_id = module.fargate_environment.fargate_security_group_id
  type                     = "ingress"
}

module "fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.8.1"

  providers = {
    aws.dns = aws
  }

  environment              = var.environment
  service_name             = var.service_name
  service_platform_version = "1.4.0"
  aws_region               = var.aws_region
  commit_sha               = "latest"
  container_port           = "8080"
  task_type                = "worker"
  desired_task_count       = 1
  task_cpu                 = 2048
  task_memory              = 4096
  maximum_capacity         = 1
  minimum_capacity         = 1
  vpc_id                   = module.vpc_info.vpc_id
  application_family       = var.application_family

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  iam_managed_policy_attachments = [
    module.efs_volume.efs_iam_policy_arn_output,
    data.aws_iam_policy.open_search_rw_policy.arn,
  ]

  additional_task_role_principals = ["arn:aws:iam::437795906767:role/prod-jenkins-aws-task-agent"]

  docker_volumes = [
    {
      name            = "${var.environment}-${var.service_name}"
      file_system_id  = module.efs_volume.efs_file_system_id_output
      access_point_id = module.efs_volume.efs_access_point_id_output
    }
  ]

  docker_volume_mount_points = [
    {
      source_volume  = "${var.environment}-${var.service_name}"
      container_path = var.efs_volume_container_path
    }
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      SWF_DOMAIN = var.swf_domain
    },
    {
      OPENSEARCH_USE_SSL = var.opensearch_use_ssl
    },
    {
      OPENSEARCH_HOST = var.opensearch_host
    },
    {
      OPENSEARCH_INDEX = var.opensearch_index
    },
    {
      OPENSEARCH_ALIAS = var.opensearch_alias
    },
    {
      LOGGER_DSN = var.logger_dsn
    },
    {
      FEED_INGESTION_TABLE = var.feed_ingestion_table
    },
    {
      SNOWFLAKE_ACCOUNT = var.snowflake_account
    },
    {
      S3_BUCKET = var.s3_bucket
    },
    {
      SNOWFLAKE_DATABASE = var.snowflake_database
    },
    {
      SNOWFLAKE_SCHEMA = var.snowflake_schema
    },
    {
      SNOWFLAKE_WAREHOUSE = var.snowflake_warehouse
    },
    {
      SNOWFLAKE_ROLE = var.snowflake_role
    },
    {
      SNOWFLAKE_USER = var.snowflake_user
    },
    {
      QUEUE_SIZE = var.queue_size
    },
    {
      THREAD_COUNT = var.thread_count
    },
  ]
  secrets = [
    {
      AWS_ACCESS_KEY_ID = "${var.environment}/${var.service_name}/AWS_ACCESS_KEY_ID"
    },
    {
      AWS_SECRET_ACCESS_KEY = "${var.environment}/${var.service_name}/AWS_SECRET_ACCESS_KEY"
    },
    {
      SNOWFLAKE_PASSWORD = "${var.environment}/${var.service_name}/SNOWFLAKE_PASSWORD"
    },
    {
      SNOWFLAKE_KEY = "${var.environment}/${var.service_name}/SNOWFLAKE_KEY"
    },
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
  ]
}

module "fargate_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service//?ref=6.15.0"
  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
}

# Create IAM user prod-swf-yt-conflict-elasticsearch.
resource "aws_iam_user" "swf-yt-conflict-elasticsearch" {
  name = "${var.environment}-${var.service_name}-user"
  tags = {
    terraformed        = true
    application_family = var.application_family
    role               = "service-user"
  }
}

# Attach iam policies to prod-swf-yt-conflict-elasticsearch iam user.
resource "aws_iam_user_policy_attachment" "iam-user-policy-attachment" {
  user       = aws_iam_user.swf-yt-conflict-elasticsearch.name
  count      = length(var.iam_policy_arn)
  policy_arn = var.iam_policy_arn[count.index]
}

module "secrets" {
  source             = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each           = toset(var.secrets_manager_secret_names)
  environment        = var.environment
  service_name       = var.service_name
  secret_name        = each.value
  application_family = var.application_family
}

resource "aws_swf_domain" "swf_domain" {
  name                                        = var.swf_domain
  workflow_execution_retention_period_in_days = 30
}
