# state machine initiated with this policy/role.
data "aws_iam_policy_document" "assume_role_for_state_machine_policy" {
  statement {
    actions = [
      "sts:AssumeRole",
    ]

    principals {
      type = "Service"

      identifiers = [
        "states.${var.region}.amazonaws.com",
      ]
    }
  }
}

resource "aws_iam_role" "state_machine_role" {
  name               = "${var.environment}-${var.state_machine_name}-state-machine-role"
  assume_role_policy = data.aws_iam_policy_document.assume_role_for_state_machine_policy.json
}

data "aws_iam_policy_document" "invoke_lambda_policy_for_state_machine" {
  statement {
    actions = [
      "lambda:InvokeFunction",
    ]
    resources = [
      module.lambda-publishing-changelog-report-populator.lambda_arn,
      module.lambda-publishing-changelog-row-builder.lambda_arn,
      module.lambda-publishing-changelog-report-compiler.lambda_arn
    ]
  }
}

# Invoke Step Function Policy
data "aws_iam_policy_document" "start_execution_state_machine_policy_doc" {
  statement {
    actions = [
      "states:StartExecution",
      "states:ListExecutions",
    ]

    resources = [
      aws_sfn_state_machine.state_machine.arn
    ]
  }
}

resource "aws_iam_policy" "start_execution_state_machine_policy" {
  name        = "StepFunctions-${var.environment}-${var.state_machine_name}-StartExecution"
  description = "Invoke Publishing Compositions Changelog state machine"
  policy      = data.aws_iam_policy_document.start_execution_state_machine_policy_doc.json
}


# Policy that gives the state machine role permission to invoke lambdas
resource "aws_iam_role_policy" "invoke_lambda_role_policy" {
  name   = "${var.environment}-${var.state_machine_name}-invoke-lambda-role-policy"
  role   = aws_iam_role.state_machine_role.id
  policy = data.aws_iam_policy_document.invoke_lambda_policy_for_state_machine.json
}

resource "aws_iam_role_policy_attachment" "policy_attachments" {
  for_each = toset([
    aws_iam_policy.start_execution_state_machine_policy.arn,
    aws_iam_policy.publishing_compositions_s3_read.arn,
    aws_iam_policy.publishing_compositions_s3_list.arn,
    aws_iam_policy.publishing_compositions_s3_write.arn,
  ])
  role       = aws_iam_role.state_machine_role.id
  policy_arn = each.key
}

data "template_file" "state_machine_definition" {
  template = file("${path.module}/changelog_report_workflow.json")

  vars = {
    # add all variables here that will be used by changelog_report_workflow.json.
    report_populator_arn = module.lambda-publishing-changelog-report-populator.lambda_arn
    report_compiler_arn  = module.lambda-publishing-changelog-report-compiler.lambda_arn
    row_builder_arn      = module.lambda-publishing-changelog-row-builder.lambda_arn
    bucket_name          = data.aws_s3_bucket.s3_bucket.bucket
  }
}

resource "aws_sfn_state_machine" "state_machine" {
  name       = "${var.environment}-${var.state_machine_name}"
  role_arn   = aws_iam_role.state_machine_role.arn
  definition = data.template_file.state_machine_definition.rendered

  tags = {
    application_family = var.application_family
    environment        = var.environment
    service_name       = var.state_machine_name
    terraformed        = true
  }

  tracing_configuration {
    enabled = true
  }
}
