provider "aws" {
  region = "us-east-1"
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/lambda-collaborator/auto-report-run/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

module "collaborator_auto_report_run" {
  source = "git@github.com:theorchard/terraform-lambda.git//?ref=3.1.9"

  application_family  = var.application_family
  use_container_image = true
  environment         = var.environment
  lambda_name         = var.lambda_name
  vpc_subnet_ids      = ["subnet-cb4dbae0", "subnet-5366ef24"]
  vpc_id              = "vpc-7f0e841a"

  lambda_description      = "Triggers automated report run for Collaborators (Direct-Payments and others)"
  lambda_function_handler = "index.handler"
  lambda_function_timeout = 900

  lambda_function_environment_variables = {
    ENVIRONMENT                    = var.environment
    LOGGER_DSN                     = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    DB_COLLABORATORS_HOST          = var.db_collaborators_host
    DB_COLLABORATORS_USERNAME      = "collab_env"
    DB_ROYALTY_ACCOUNTING_HOST     = var.db_royalty_accounting_host
    DB_ROYALTY_ACCOUNTING_USERNAME = "lambda-collaborator"
    DB_ART_RELATIONS_HOST          = var.db_art_relations_host
    DB_ART_RELATIONS_USERNAME      = "lambda-collaborator-report"
    SQS_QUEUE_URL                  = data.aws_sqs_queue.sqs_queue.url
    DD_ENV                         = var.environment
    DD_SERVICE                     = var.lambda_name
  }

  datadog_advanced_enabled                 = true
  datadog_function_destination_lambda_name = "DatadogLambdaFunction"
}

data "aws_iam_policy_document" "lambda_execution_iam_policy_document" {
  statement {
    actions = [
      "secretsmanager:GetSecretValue",
    ]

    resources = [for secret in module.secrets : secret.secret_arn]
  }

  statement {
    actions = [
      "sqs:SendMessage"
    ]

    resources = [
      data.aws_sqs_queue.sqs_queue.arn
    ]
  }
}

resource "aws_iam_role_policy" "lambda_execution_iam_policy" {
  role   = module.collaborator_auto_report_run.lambda_role_id
  policy = data.aws_iam_policy_document.lambda_execution_iam_policy_document.json
}

data "aws_iam_policy_document" "lambda_invoke_policy_document" {
  statement {
    effect = "Allow"
    actions = [
      "lambda:InvokeFunction"
    ]
    resources = [
      module.collaborator_auto_report_run.lambda_arn
    ]
  }
}

resource "aws_iam_policy" "lambda_invoke_policy" {
  name        = "${var.environment}-${var.lambda_name}-invoke-policy"
  description = "Policy for invoking ${var.lambda_name} lamdba"
  policy      = data.aws_iam_policy_document.lambda_invoke_policy_document.json
}

data "aws_sqs_queue" "sqs_queue" {
  name = var.sqs_queue_name
}

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.lambda_name
  secret_name        = each.value
  application_family = var.application_family
}

module "sentry_project" {
  source             = "git@github.com:theorchard/terraform-sentry//?ref=4.1.0"
  service_name       = var.lambda_name
  environment        = var.environment
  platform           = "python"
  application_family = var.application_family
}
