resource "aws_batch_job_definition" "read_input_data_job_definition" {
  name = "${var.environment}-${var.service_name}-read-input-data-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 900 # 15 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP1" },
      { "name" = "CHUNK_COUNT", "value" = "10" },
      { "name" = "FOLDER", "value" = "input" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "SNOWFLAKE_PASSWORD_SECRET", "value" = "${var.environment}/${var.service_name}/SNOWFLAKE_PASSWORD" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
      { "name" : "SNOWFLAKE_USERNAME", "value" : "MOMENTS_PIPELINE" },
      { "name" : "SNOWFLAKE_WAREHOUSE", "value" : "PROD_MOMENTS" },
    ]

    # this is a potential improvement, we can delegate value retrival
    secrets = [
      { "name" = "SNOWFLAKE_PASSWORD", "valueFrom" = module.ds_moments_secrets["SNOWFLAKE_PASSWORD"].secret_arn }
    ]


    resourceRequirements = [
      {
        type  = "VCPU"
        value = "1"
      },
      {
        type  = "MEMORY"
        value = "3072"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "fourier_transform_job_definition" {
  name = "${var.environment}-${var.service_name}-fourier-transform-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 1800 # 30 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP2" },
      { "name" = "CHUNK_COUNT", "value" = "10" },
      { "name" = "FOLDER", "value" = "input" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "2"
      },
      {
        type  = "MEMORY"
        value = "4096"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "arima_job_definition" {
  name = "${var.environment}-${var.service_name}-arima-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 7200 # 120 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP3" },
      { "name" = "CHUNK_COUNT", "value" = "10" },
      { "name" = "FOLDER", "value" = "input" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "8"
      },
      {
        type  = "MEMORY"
        value = "16384"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "arima_cross_merge_job_definition" {
  name = "${var.environment}-${var.service_name}-arima-cross-merge-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 900 # 15 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP4" },
      { "name" = "FOLDER", "value" = "input" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "1"
      },
      {
        type  = "MEMORY"
        value = "3072"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "arima_timeseries_merge_job_definition" {
  name = "${var.environment}-${var.service_name}-arima-timeseries-merge-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 900 # 15 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP5" },
      { "name" = "FOLDER", "value" = "input" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "1"
      },
      {
        type  = "MEMORY"
        value = "3072"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "clustering_job_definition" {
  name = "${var.environment}-${var.service_name}-clustering-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 900 # 15 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP6" },
      { "name" = "FOLDER", "value" = "input" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "2"
      },
      {
        type  = "MEMORY"
        value = "4096"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "copy_data_to_s3_job_definition" {
  name = "${var.environment}-${var.service_name}-copy-to-s3-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 900 # 15 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP7" },
      { "name" = "FOLDER", "value" = "input" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "SNOWFLAKE_PASSWORD_SECRET", "value" = "${var.environment}/${var.service_name}/SNOWFLAKE_PASSWORD" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
      { "name" : "SNOWFLAKE_USERNAME", "value" : "MOMENTS_PIPELINE" },
      { "name" : "SNOWFLAKE_WAREHOUSE", "value" : "PROD_MOMENTS" },
      { "name" : "SNOWFLAKE_ARIMA_RESULT_TABLE", "value" : "ARIMA_TABLE_TEST" },
    ]

    secrets = [
      { "name" = "SNOWFLAKE_PASSWORD", "valueFrom" = module.ds_moments_secrets["SNOWFLAKE_PASSWORD"].secret_arn }
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "2"
      },
      {
        type  = "MEMORY"
        value = "4096"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "moments_script_job_definition" {
  name = "${var.environment}-${var.service_name}-single-script-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 18000 # 5 hours
  }

  container_properties = jsonencode({
    image            = local.ecs_repo_single_script
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "CHUNK_COUNT", "value" = "10" },
      { "name" = "FOLDER", "value" = "input-test" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "8"
      },
      {
        type  = "MEMORY"
        value = "16384"
      }
    ]
  })

  propagate_tags = true
}

resource "aws_batch_job_definition" "copy_fourier_data_to_s3_job_definition" {
  name = "${var.environment}-${var.service_name}-copy-fourier-data-to-s3-job-definition"
  type = "container"

  platform_capabilities = [
    "FARGATE",
  ]

  timeout {
    attempt_duration_seconds = 900 # 15 minutes
  }

  container_properties = jsonencode({
    image            = local.ecs_repo_single_script
    executionRoleArn = aws_iam_role.aws_ecs_task_execution_role.arn
    jobRoleArn       = aws_iam_role.aws_batch_job_role.arn

    fargatePlatformConfiguration = {
      platformVersion = "LATEST"
    }

    environment = [
      { "name" = "STEP", "value" = "STEP7" },
      { "name" = "FOLDER", "value" = "input-test" }, # the path shouldn't start with a / and shouldn't not end with a /
      { "name" = "RUN_ID", "value" = "1" },
      { "name" = "SNOWFLAKE_PASSWORD_SECRET", "value" = "${var.environment}/${var.service_name}/SNOWFLAKE_PASSWORD" },
      { "name" = "BUCKET_NAME", value = module.ds_moments_s3_bucket.s3_bucket_name_output },
      { "name" : "SNOWFLAKE_USERNAME", "value" : "MOMENTS_PIPELINE" },
      { "name" : "SNOWFLAKE_WAREHOUSE", "value" : "PROD_MOMENTS" },
    ]

    secrets = [
      { "name" = "SNOWFLAKE_PASSWORD", "valueFrom" = module.ds_moments_secrets["SNOWFLAKE_PASSWORD"].secret_arn }
    ]

    resourceRequirements = [
      {
        type  = "VCPU"
        value = "2"
      },
      {
        type  = "MEMORY"
        value = "4096"
      }
    ]
  })

  propagate_tags = true
}
