# Step Functions State Machine definition for sfn-content-match-audio.

locals {
  sfn_name = "${var.sfn_name_prefix}-${var.service_name}"
  lambda_prefix = "arn:aws:lambda:${var.aws_region}:${data.aws_caller_identity.current.account_id}:function:${var.environment}-${var.lambda_name_prefix}-${var.service_name}-"
  save_audio_matches_table_name = "${var.environment}_content_review_${replace(var.service_name, "-", "_")}_match"
}

# Step Function (SFN) root role policy document.
data "aws_iam_policy_document" "sfn_content_match_audio_assume_role" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type = "Service"
      identifiers = [
        "states.amazonaws.com",
        "lambda.amazonaws.com",
        "events.amazonaws.com",
      ]
    }
  }
}
# create iam role
resource "aws_iam_role" "sfn_content_match_audio_assume_role" {
  name               = "${var.environment}-${local.sfn_name}-state-machine-role"
  assume_role_policy = data.aws_iam_policy_document.sfn_content_match_audio_assume_role.json
}


# Step Function (SFN) - SFN Cloudwatch logs permission policy
data "aws_iam_policy_document" "sfn_content_match_audio_log_event_policy_document" {
  statement {
    effect = "Allow"
    resources = [
      "arn:aws:logs:${var.aws_region}:${data.aws_caller_identity.current.account_id}:log-group:${var.environment}-${local.sfn_name}",
      "arn:aws:logs:${var.aws_region}:${data.aws_caller_identity.current.account_id}:log-group:${var.environment}-${local.sfn_name}:log-stream:*"
    ]

    actions = [
      "logs:CreateLogGroup",
      "logs:CreateLogStream",
      "logs:PutLogEvents"
    ]
  }
}
resource "aws_iam_role_policy" "sfn_content_match_audio_log_event_policy_attachment" {
  name   = "${var.environment}-${local.sfn_name}-log-event-policy"
  role   = aws_iam_role.sfn_content_match_audio_assume_role.id
  policy = data.aws_iam_policy_document.sfn_content_match_audio_log_event_policy_document.json
}


# State machine lambda policy that gives the state machine role permission to invoke lambdas
data "aws_iam_policy_document" "sfn_content_match_audio_invoke_lambda_policy_document" {
  statement {
    actions = [
      "lambda:InvokeFunction",
    ]

    resources = [
      "${local.lambda_prefix}*"
    ]
  }
}
# attach invoke lambda policy.
resource "aws_iam_role_policy" "sfn_content_match_audio_invoke_lambda_role_policy" {
  name   = "${var.environment}-${local.sfn_name}-lambda-role-policy"
  role   = aws_iam_role.sfn_content_match_audio_assume_role.id
  policy = data.aws_iam_policy_document.sfn_content_match_audio_invoke_lambda_policy_document.json
}

# Get existing dynamodb table
data "aws_dynamodb_table" "save_audio_matches_table" {
  name = local.save_audio_matches_table_name
}
# State machine dynamodb policy that gives the state machine role permission to PutItem to dynamodb
data "aws_iam_policy_document" "sfn_content_match_audio_dynamodb_put_item_policy_document" {
  statement {
    actions = [
      "dynamodb:PutItem",
    ]

    resources = [
      data.aws_dynamodb_table.save_audio_matches_table.arn
    ]
  }
}
# attach dynamodb:PutItem policy.
resource "aws_iam_role_policy" "sfn_content_match_audio_dynamodb_put_item_role_policy" {
  name   = "${var.environment}-${local.sfn_name}-dynamodb-put-item-policy"
  role   = aws_iam_role.sfn_content_match_audio_assume_role.id
  policy = data.aws_iam_policy_document.sfn_content_match_audio_dynamodb_put_item_policy_document.json
}


# Template for state machine.
data "template_file" "sfn_content_match_audio_definition" {
  template = file("${path.module}/${local.sfn_name}.json")

  vars = {
    lambda_prefix = local.lambda_prefix
    fingerprint_audio = "FingerprintAudio",
    match_audio = "MatchAudio",
    save_audio_matches = "SaveAudioMatches",
    save_audio_matches_table_name = local.save_audio_matches_table_name
  }
}
resource "aws_sfn_state_machine" "sfn_content_match_audio" {
  name       = "${var.environment}-${local.sfn_name}"
  role_arn   = aws_iam_role.sfn_content_match_audio_assume_role.arn
  definition = data.template_file.sfn_content_match_audio_definition.rendered
  tags = {
    application_family = var.application_family
    environment        = var.environment
    service_name       = local.sfn_name
    terraformed        = true
  }
  tracing_configuration {
    enabled = true
  }
}
