# 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}-nr-test-sfn-state-machine-role"
  assume_role_policy = data.aws_iam_policy_document.assume_role_for_state_machine_policy.json
}

# policy to give the state machine role permission to invoke any lambda
data "aws_iam_policy_document" "policy_for_state_machine" {
  statement {
    actions = [
      "lambda:InvokeFunction",
    ]
    resources = [
      "arn:aws:lambda:us-east-1:103233932089:function:dev-nr-test-lambda:*"
    ]
  }

  statement {
    effect    = "Allow"
    resources = [
      "arn:aws:s3:::dev-nr-test",
    ]

    actions = [
      "s3:ListBucket"
    ]

    condition {
      test     = "StringLike"
      values   = ["dev-nr-test"]
      variable = "s3:prefix"
    }
  }

  statement {
    effect    = "Allow"
    resources = [
      "arn:aws:s3:::dev-nr-test/*",
    ]

    actions = [
      "s3:PutObject",
      "s3:GetObject",
      "s3:ListMultipartUploadParts",
      "s3:AbortMultipartUpload"
    ]
  }
}

data "aws_iam_policy_document" "s3_write" {
  statement {
    effect    = "Allow"
    resources = [
      "arn:aws:s3:::dev-nr-test/*",
    ]

    actions = [
      "s3:PutObject",
      "s3:GetObject",
      "s3:ListMultipartUploadParts",
      "s3:AbortMultipartUpload"
    ]
  }
}

# Policy that gives the state machine role permission to start Distributed Map Executions
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}-nr-test-sfn-StartExecution"
  description = "Invoke Distributed Map Child State Machines"
  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}-nr-test-sfn-invoke-lambda-role-policy"
  role   = aws_iam_role.state_machine_role.id
  policy = data.aws_iam_policy_document.policy_for_state_machine.json
}

resource "aws_iam_role_policy_attachment" "other_policy_attachment" {
  role       = aws_iam_role.state_machine_role.id
  policy_arn = aws_iam_policy.start_execution_state_machine_policy.arn
}

resource "aws_iam_policy" "s3_write" {
  name   = "dev-nr-test-lambda-s3-write-policy"
  policy = data.aws_iam_policy_document.s3_write.json
}

resource "aws_iam_role_policy_attachment" "test_lambda_s3_attachment" {
  role = "dev-nr-test-lambda-role-8xwd795e"
  policy_arn = aws_iam_policy.s3_write.arn
}

data "template_file" "state_machine_definition" {
  // empty state function def - will be updated in dev using the console
  template = <<INPUT_TEMPLATE_EOF
{
  "StartAt": "EmptyState",
  "States": {
    "EmptyState": {
      "Type": "Pass",
      "End": true
    }
  }
}
    INPUT_TEMPLATE_EOF
}

resource "aws_sfn_state_machine" "state_machine" {
  name       = "${var.environment}-nr-test-sfn"
  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       = "test-sfn"
    terraformed        = true
  }

  tracing_configuration {
    enabled = true
  }
}
