variable "aws_region" {
  default     = "us-east-1"
  description = "AWS region"
}

variable "environment" {
  description = "Available values:  dev, qa, prod."
  default     = "prod"
}

variable "service_name" {
  default = "ds-moments-pipeline"
}

variable "application_family" {
  default = "business-intelligence"
}

variable "team_name" {
  default = "devops"
}

variable "bucket_name" {
  default     = "ds-moments"
  description = "bucket used for storing project related data"
}

variable "secrets_manager_secret_names" {
  description = "List of secrets manager secrets to create"
  type        = list(string)
  default = [
    "SNOWFLAKE_PASSWORD",
  ]
}

variable "stage_snowflake_database" {
  default = "INTELLIGENCE"
}

variable "stage_snowflake_schema" {
  default = "DBT_PROD_PROJECT_MOMENTS"
}

variable "stage_snowflake_role" {
  default = "MOMENTS_PIPELINE_ROLE"
}

variable "github_repository" {
  default = "ds-moments"
}

locals {
  ecs_repo = "${module.aws_accounts.orcd_accounts["shared"]}.dkr.ecr.${var.aws_region}.amazonaws.com/${var.github_repository}:latest"
  ecs_repo_single_script = "${module.aws_accounts.orcd_accounts["shared"]}.dkr.ecr.${var.aws_region}.amazonaws.com/${var.github_repository}:script"

  step_1_name = "read-input-data"
  step_2_name = "fourier_transform"
  step_3_name = "run_arima"
  step_4_name = "arima_cross_merge"
  step_5_name = "arima_timeseries_merge"
  step_6_name = "run_clustering"
  step_7_name = "copy_data_to_s3"
  chunk_count = 10

  sfn_name = var.service_name

  override_environment = [
    {
      "Name" : "CHUNK_COUNT",
      "Value" : "10"
    },
    {
      "Name" : "FOLDER",
      "Value.$" : "States.Format('input-{}', States.ArrayGetItem(States.StringSplit($.time, 'T'), 0))"
    },
    {
      "Name" : "RUN_ID",
      "Value" : "1"
    },
    {
      "Name" : "BUCKET_NAME",
      "Value" : module.ds_moments_s3_bucket.s3_bucket_name_output
    },
    {
      "Name" : "SNOWFLAKE_PASSWORD_SECRET",
      "Value" : module.ds_moments_secrets["SNOWFLAKE_PASSWORD"].secret_name
    },
    {
      "Name" : "SNOWFLAKE_USERNAME",
      "Value" : "MOMENTS_PIPELINE"
    },
    {
      "Name" : "SNOWFLAKE_WAREHOUSE",
      "Value" : "PROD_MOMENTS"
    },
    {
      "Name" : "SNOWFLAKE_ARIMA_RESULT_TABLE",
      "Value" : "ARIMA_TABLE_TEST"
    },
  ]

  override__single_script_environment = [
    {
      "Name" : "CHUNK_COUNT",
      "Value" : "10"
    },
    {
      "Name" : "FOLDER",
      "Value.$" : "States.Format('input-test-{}', States.ArrayGetItem(States.StringSplit($.time, 'T'), 0))"
    },
    {
      "Name" : "RUN_ID",
      "Value" : "1"
    },
    {
      "Name" : "BUCKET_NAME",
      "Value" : module.ds_moments_s3_bucket.s3_bucket_name_output
    },
    {
      "Name" : "SNOWFLAKE_PASSWORD_SECRET",
      "Value" : module.ds_moments_secrets["SNOWFLAKE_PASSWORD"].secret_name
    },
    {
      "Name" : "SNOWFLAKE_USERNAME",
      "Value" : "MOMENTS_PIPELINE"
    },
    {
      "Name" : "SNOWFLAKE_WAREHOUSE",
      "Value" : "PROD_MOMENTS"
    },
  ]

  snf_state_machine_definition = jsonencode({
    "Comment" = "ds-moments pipeline",
    "StartAt" = local.step_1_name,
    "States" = {
      (local.step_1_name) = {
        "Type"     = "Task",
        "Resource" = "arn:aws:states:::batch:submitJob.sync",
        "Parameters" : {
          "JobName"       = local.step_1_name,
          "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
          "JobDefinition" = aws_batch_job_definition.read_input_data_job_definition.arn
          "ContainerOverrides" : {
            "Environment" : concat(
              local.override_environment,
              [
                {
                  Name : "STEP",
                  Value : "STEP1"
                },
              ]
            )
          }
        },
        "ResultPath" : null,
        "Next" : local.step_2_name,
      },
      (local.step_2_name) = {
        "Type"     = "Task",
        "Resource" = "arn:aws:states:::batch:submitJob.sync",
        "Parameters" = {
          "JobName"       = local.step_2_name,
          "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
          "JobDefinition" = aws_batch_job_definition.fourier_transform_job_definition.arn,
          "ArrayProperties" = {
            "Size.$" = "States.StringToJson('${local.chunk_count}')"
          },
          "ContainerOverrides" : {
            "Environment" : concat(
              local.override_environment,
              [
                {
                  Name : "STEP",
                  Value : "STEP2"
                },
              ]
            )
          }
        },
        "ResultPath" : null,
        "Next" : local.step_3_name,
      },
      (local.step_3_name) = {
        "Type"     = "Task",
        "Resource" = "arn:aws:states:::batch:submitJob.sync",
        "Parameters" = {
          "JobName"       = local.step_3_name,
          "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
          "JobDefinition" = aws_batch_job_definition.arima_job_definition.arn,
          "ArrayProperties" = {
            "Size.$" = "States.StringToJson('${local.chunk_count}')"
          },
          "ContainerOverrides" : {
            "Environment" : concat(
              local.override_environment,
              [
                {
                  Name : "STEP",
                  Value : "STEP3"
                },
              ]
            )
          }
        },
        "ResultPath" : null,
        "Next" : "Aggregations",
      },
      "Aggregations" : {
        "Type" : "Parallel",
        "Next" : local.step_6_name,
        "ResultPath" : null,
        "Branches" : [
          {
            "StartAt" : local.step_4_name,
            "States" : {
              (local.step_4_name) : {
                "Type"     = "Task",
                "Resource" = "arn:aws:states:::batch:submitJob.sync",
                "Parameters" = {
                  "JobName"       = local.step_4_name,
                  "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
                  "JobDefinition" = aws_batch_job_definition.arima_cross_merge_job_definition.arn,
                  "ContainerOverrides" : {
                    "Environment" : concat(
                      local.override_environment,
                      [
                        {
                          Name : "STEP",
                          Value : "STEP4"
                        },
                      ]
                    )
                  }
                },
                "End" : true,
                "ResultPath" : null,
              }
            }
          },
          {
            "StartAt" : local.step_5_name,
            "States" : {
              (local.step_5_name) : {
                "Type"     = "Task",
                "Resource" = "arn:aws:states:::batch:submitJob.sync",
                "Parameters" = {
                  "JobName"       = local.step_5_name,
                  "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
                  "JobDefinition" = aws_batch_job_definition.arima_timeseries_merge_job_definition.arn,
                  "ContainerOverrides" : {
                    "Environment" : concat(
                      local.override_environment,
                      [
                        {
                          Name : "STEP",
                          Value : "STEP5"
                        },
                      ]
                    )
                  }
                },
                "End" : true,
                "ResultPath" : null,
              }
            }
          }
        ]
      },
      (local.step_6_name) = {
        "Type"     = "Task",
        "Resource" = "arn:aws:states:::batch:submitJob.sync",
        "Parameters" = {
          "JobName"       = local.step_6_name,
          "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
          "JobDefinition" = aws_batch_job_definition.clustering_job_definition.arn,
          "ContainerOverrides" : {
            "Environment" : concat(
              local.override_environment,
              [
                {
                  Name : "STEP",
                  Value : "STEP6"
                },
              ]
            )
          }
        },
        "Next" : local.step_7_name,
        "ResultPath" : null,
      },
      (local.step_7_name) = {
        "Type"     = "Task",
        "Resource" = "arn:aws:states:::batch:submitJob.sync",
        "Parameters" = {
          "JobName"       = local.step_7_name,
          "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
          "JobDefinition" = aws_batch_job_definition.copy_data_to_s3_job_definition.arn,
          "ContainerOverrides" : {
            "Environment" : concat(
              local.override_environment,
              [
                {
                  Name : "STEP",
                  Value : "STEP7"
                },
              ]
            )
          }
        },
        "End" : true,
        "ResultPath" : null,
      },
    }
  })

  snf_state_machine_single_script_definition = jsonencode({
    "Comment" = "ds-moments pipeline single script",
    "StartAt" = "fourier_transform",
    "States" = {
      "fourier_transform" = {
        "Type"     = "Task",
        "Resource" = "arn:aws:states:::batch:submitJob.sync",
        "Parameters" = {
          "JobName"       = "fourier_transform",
          "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
          "JobDefinition" = aws_batch_job_definition.moments_script_job_definition.arn,
          "ArrayProperties" = {
            "Size.$" = "States.StringToJson('${local.chunk_count}')"
          },
          "ContainerOverrides" : {
            "Environment" : concat(
              local.override__single_script_environment,
              [
                {
                  Name : "STEP",
                  Value : "STEP1"
                },
              ]
            )
          }
        },
        "Next" : "copy_fourier_data_to_s3",
        "ResultPath" : null,
      },
      "copy_fourier_data_to_s3" = {
        "Type"     = "Task",
        "Resource" = "arn:aws:states:::batch:submitJob.sync",
        "Parameters" = {
          "JobName"       = "copy_fourier_data_to_s3",
          "JobQueue"      = aws_batch_job_queue.batch_compute_environment.arn,
          "JobDefinition" = aws_batch_job_definition.copy_fourier_data_to_s3_job_definition.arn,
          "ContainerOverrides" : {
            "Environment" : concat(
              local.override__single_script_environment,
              [
                {
                  Name : "STEP",
                  Value : "STEP2"
                },
              ]
            )
          }
        },
        "End" : true,
        "ResultPath" : null,
      }
    }
  })

  snowflake_integration_role_name = "${var.environment}-${var.application_family}-snowflake-integration-role"
}

variable "notification_endpoints" {
  default = "@slack-analytics-team-monitoring"
}

variable "escalation_notification_endpoints" {
  default = "@slack-analytics-team-monitoring"
}
