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 backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/vector/obtain_encoding_orders/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

  environment = var.environment
}

# Lookup existing KMS policy
data "aws_iam_policy" "kms_policy" {
  name = "KMS-${var.environment}-vector-encoder-policy"
}

data "aws_iam_policy_document" "s3_nickelback_obtain_encoding_orders_ringtone_ro_policy" {
  statement {
    effect = "Allow"
    actions = [
      "s3:GetBucketLocation",
      "s3:GetObject"
    ]
    resources = [
      "arn:aws:s3:::nickelback/${var.service_name}/${var.environment}/*"
    ]
  }

  statement {
    effect = "Allow"
    actions = [
      "s3:ListBucket"
    ]
    resources = [
      "arn:aws:s3:::nickelback"
    ]
  }

  statement {
    effect = "Allow"
    actions = [
      "s3:ListAllMyBuckets",
      "s3:GetBucketLocation"
    ]
    resources = [
      "*"
    ]
  }
}

resource "aws_iam_policy" "s3_configs_read_only_policy" {
  name   = "S3-${var.environment}-${var.service_name}-RO"
  policy = data.aws_iam_policy_document.s3_nickelback_obtain_encoding_orders_ringtone_ro_policy.json
}

module "vector_cron_worker_sentry_project" {
  source             = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"
  service_name       = var.service_name
  environment        = var.environment
  teams              = [var.environment]
  application_family = var.application_family
  platform           = "php"
}

# Vector Cron Worker
module "obtain_ringtone_encoding_orders_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.5.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                           = var.environment
  service_name                          = var.service_name
  non_ecr_image                         = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/vector-worker:latest"
  vpc_id                                = module.vpc_info.vpc_id
  application_family                    = var.application_family
  secrets_manager_service_name          = var.service_name
  aws_region                            = var.aws_region
  commit_sha                            = "latest"
  task_type                             = "worker"
  health_check_command                  = "pgrep php"
  desired_task_count                    = 0
  task_cpu                              = 512
  task_memory                           = 1024
  minimum_capacity                      = 0
  maximum_capacity                      = 0
  task_protection_policy_enabled        = true
  splitio_enabled                       = false
  ows_machine_to_machine_enabled        = false
  autoscaling_cpu_policy_enabled        = false
  autoscaling_memory_policy_enabled     = false
  external_autoscaling_policy_enabled   = false
  stopped_task_monitoring_enabled       = true
  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = true
  cloudwatch_event_enabled              = true
  cloudwatch_event_schedule             = "rate(4 hours)"
  iam_managed_policy_attachments = [
    data.aws_iam_policy.kms_policy.arn,
    aws_iam_policy.s3_configs_read_only_policy.arn,
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      PHP_ENTRYPOINT = "scripts/obtain_encoding_orders.php"
    },
    {
      LOCAL_SOURCE = "/var/app/source/"
    },
    {
      LOG_LEVEL = "7"
    },
    {
      STDOUT = "1"
    },
  ]
  secrets = [
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    }
  ]
}

