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.application_family
}

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  = "dev-orcd-terraform-state"
    key     = "dev/fansifter-video-clipper/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

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

  environment = var.environment
}

data "aws_route53_zone" "route53_zone" {
  name = "dev.theorchard.io"
}

data "aws_route53_zone" "networking_route53_zone" {
  provider = aws.networking
  name     = "theorchard.io"
}

module "web_service_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

  desired_task_count                       = 1
  minimum_capacity                         = 1
  maximum_capacity                         = 1
  task_cpu                                 = 4096
  task_memory                              = 8192
  task_ephemeral_storage_size              = 20
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = module.vpc_info.vpc_id
  fargate_service_subnets                  = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                    = module.vpc_info.default_private_subnet_ids

  https_listener_allow_prefix_list_names = [
    "vpn-ny-users",
  ]

  container_port    = 8080
  health_check_path = "/api/v1/utils/health-check/"
  non_ecr_image     = "103233932089.dkr.ecr.us-east-1.amazonaws.com/fansifter-video-clipper:latest"

  splitio_enabled                = false
  ows_machine_to_machine_enabled = false

  iam_managed_policy_attachments = [
    module.efs_volume.efs_iam_policy_arn_output,
  ]

  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 = "/mnt/efs"
    }
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      ENVIRONMENT = var.environment
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      SENTRY_DSN = module.sentry.sentry_key_dsn_public_output
    },
    {
      POSTGRES_SERVER = module.postgres_rds.rds_cluster_endpoint
    },
    {
      POSTGRES_PORT = module.postgres_rds.rds_cluster_port
    },
    {
      POSTGRES_USER = "clipper_user"
    },
    {
      POSTGRES_DB = "fansifter_video_clipper"
    },
    {
      REDIS_HOST = module.redis_elasticache.redis_primary_endpoint_address
    },
    {
      REDIS_PORT = "6379"
    },
    {
      FIRST_SUPERUSER = "admin@${var.environment}.theorchard.io"
    },
    {
      PROJECT_NAME = "Fansifter Clipper"
    },
    {
      MEDIA_ROOT = "/mnt/efs"
    },
    {
      FRONTEND_HOST = "https://${var.environment}-${var.service_name}.${data.aws_route53_zone.route53_zone.name}"
    },
  ]

  secrets = [
    {
      POSTGRES_PASSWORD = "${var.environment}/${var.service_name}/POSTGRES_PASSWORD"
    },
    {
      SECRET_KEY = "${var.environment}/${var.service_name}/SECRET_KEY"
    },
    {
      FIRST_SUPERUSER_PASSWORD = "${var.environment}/${var.service_name}/FIRST_SUPERUSER_PASSWORD"
    },
    {
      GEMINI_API_KEY = "${var.environment}/${var.service_name}/GEMINI_API_KEY"
    },
  ]
}

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
}

module "fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.15.2"

  environment        = var.environment
  environment_type   = "fargate"
  service_name       = var.service_name
  application_family = var.application_family
  teams              = [var.application_family]

  service_4xx_monitor_enabled = false
  service_cpu_monitor_enabled = false

  notification_endpoints            = "@slack-fansifter-alerts"
  escalation_notification_endpoints = "@slack-fansifter-alerts"

  healthy_tasks_warning_number          = 0.2
  healthy_tasks_warning_recovery_number = 1
  healthy_tasks_ok_number               = 1
}

module "sentry" {
  source = "git@github.com:theorchard/terraform-sentry.git?ref=5.0.0"

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  application_family = var.application_family
}
