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.config_service_name
  team_name          = var.team_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/vector/generate_video_encoding_orders/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

# Policy for read-only access to the S3 bucket
data "aws_iam_policy" "s3_configs_read_only_policy" {
  name = "S3-${var.environment}-${var.config_service_name}-RO"
}

# Policy for KMS access to encrypted config objects in the S3 bucket
data "aws_iam_policy" "orchard_repo_cron_worker_kms_policy" {
  name = "KMS-${var.environment}-${var.config_service_name}-policy"
}

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

  environment = var.environment
}

module "generate_video_encoding_order_sns_topic" {
  source = "git@github.com:theorchard/terraform-sns.git//?ref=3.0.0"

  environment              = var.environment
  sns_topic_name           = "video_catalog"
  application_family       = var.application_family
  sns_subscription_enabled = false
}

data "aws_elasticache_replication_group" "split_synchronizer_redis_cache" {
  replication_group_id = "${var.environment}-split-synchronizer"
}

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

  providers = {
    aws.dns = aws
  }

  environment                        = var.environment
  service_name                       = var.service_name
  aws_region                         = var.aws_region
  application_family                 = var.application_family
  non_ecr_image                      = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.config_service_name}:latest"
  task_type                          = "worker"
  health_check_command               = "pgrep php"
  deployment_minimum_healthy_percent = "100"
  autoscaling_cpu_policy_enabled     = false
  autoscaling_memory_policy_enabled  = false
  desired_task_count                 = 0
  minimum_capacity                   = var.minimum_capacity
  maximum_capacity                   = var.maximum_capacity
  task_cpu                           = 2048
  task_memory                        = 4096
  container_port                     = 8080
  health_check_grace_period_seconds  = 300
  target_deregistration_delay        = 300
  container_start_period_seconds     = 300
  task_protection_policy_enabled     = true
  cloudwatch_event_enabled           = true
  cloudwatch_event_schedule          = "rate(1 hour)"
  vpc_id                             = module.vpc_info.vpc_id
  fargate_service_subnets            = module.vpc_info.default_private_subnet_ids
  iam_managed_policy_attachments = [
    data.aws_iam_policy.orchard_repo_cron_worker_kms_policy.arn,
    data.aws_iam_policy.s3_configs_read_only_policy.arn,
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      APPLICATION_ENV = "production"
    },
    {
      DD_TRACE_ANALYTICS_ENABLED = "true"
    },
    {
      DD_TRACE_APP_NAME = var.service_name
    },
    {
      DD_TRACE_GLOBAL_TAGS = "env:${var.environment}"
    },
    {
      PATH_FROM_REPO_ROOT_OF_PHP_SCRIPT_TO_RUN = "scripts/encodingorder/generate_video_encoding_orders.php"
    },
    {
      PHP_DISPLAY_ERRORS = "false"
    },
    {
      SNS_VIDEO_ENCODING_ORDERS_TOPIC_ARN = module.generate_video_encoding_order_sns_topic.topic_arn
    },
    {
      S3_CONFIG_LOCATION = "${var.config_s3_bucket}/${var.config_service_name}/${var.environment}"
    },
    {
      LC_ALL = "en_US.UTF-8"
    },
    {
      LANG = "en_US.UTF-8"
    },
    {
      LANGUAGE = "en_US.UTF-8"
    },
    {
      SENTRY_DSN = "https://c0e8157de78d448ca46774ca98f7c094@o22178.ingest.us.sentry.io/67618"
    },
    {
      SPLITIO_REDIS_HOST = data.aws_elasticache_replication_group.split_synchronizer_redis_cache.primary_endpoint_address
    },
    {
      SPLITIO_REDIS_PORT = data.aws_elasticache_replication_group.split_synchronizer_redis_cache.port
    },
  ]
  secrets = [
    {
      SPLITIO_API_KEY = "${var.environment}/split/API_KEY"
    },
  ]
}

module "m2m_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//modules/auth0m2m?ref=1.6.1"

  environment        = var.environment
  service_name       = var.service_name
  application_family = var.application_family
}
