module "ows_product_staging_s3_bucket" {
  source = "git@github.com:theorchard/terraform-s3.git//modules/s3_bucket?ref=3.15.7"

  env                = var.environment
  bucket_name        = var.service_name
  application_family = var.application_family
  apply_server_side_encryption_by_default = {
    sse_algorithm = "AES256"
  }

  acceleration_status = "Enabled"

  # CORS Rules
  bucket_cors_rule = [
    {
      allowed_headers = ["*"]
      allowed_methods = ["PUT", "POST"]
      allowed_origins = [
        "https://content.qaorch.com",
        "https://workstation.qaorch.com",
        "http://localhost:8080"
      ]
      expose_headers  = ["ETag"]
      max_age_seconds = 3000
    }
  ]

  lifecycle_rules_abort_incomplete_multipart_upload_days = [
    {
      prefix  = ""
      enabled = true
      days    = 7
    }
  ]

  enable_guardduty_malware_protection = true
}

module "datadog" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/s3?ref=6.16.2"

  bucket_name                       = module.ows_product_staging_s3_bucket.s3_bucket_name_output
  environment                       = var.environment
  service_name                      = var.service_name
  application_family                = var.application_family
  teams                             = ["content-creation-management"]
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints

  dashboard_configuration = {
    enabled                       = false
    show_advanced_storage_metrics = false
    show_replication_metrics      = false
    show_activity_metrics         = false
  }

  guardduty_malware_monitor_configuration = {
    enabled = true
  }
}

data "aws_iam_policy_document" "ows_product_staging_s3_bucket_rw_policy_document" {
  statement {
    effect = "Allow"
    actions = [
      "s3:ListBucket",
    ]
    resources = [
      module.ows_product_staging_s3_bucket.s3_bucket_arn_output
    ]
  }
  statement {
    actions = [
      "s3:GetObject",
      "s3:PutObject",
      "s3:DeleteObject",
    ]

    resources = [
      "${module.ows_product_staging_s3_bucket.s3_bucket_arn_output}/*",
    ]
  }
}

resource "aws_iam_policy" "ows_product_staging_s3_bucket_rw_policy" {
  name   = "S3-${var.environment}-ows-product-staging-bucket-RW"
  policy = data.aws_iam_policy_document.ows_product_staging_s3_bucket_rw_policy_document.json
  tags = {
    environment        = var.environment
    service_name       = var.service_name
    application_family = var.application_family
    terraformed        = true
  }
}

//SYS-27351 temporary permissions
data "aws_iam_policy_document" "ows_product_staging_s3_bucket_delete_policy_document" {
  statement {
    effect = "Allow"
    actions = [
      "s3:ListBucket",
      "s3:GetBucket*",
    ]
    resources = [
      module.ows_product_staging_s3_bucket.s3_bucket_arn_output
    ]
    condition {
      test     = "Bool"
      variable = "aws:MultiFactorAuthPresent"
      values = [
        "true",
      ]
    }
  }
  statement {
    actions = [
      "s3:GetObject*",
      "s3:DeleteObject*",
    ]

    resources = [
      "${module.ows_product_staging_s3_bucket.s3_bucket_arn_output}/*",
    ]
    condition {
      test     = "Bool"
      variable = "aws:MultiFactorAuthPresent"
      values = [
        "true",
      ]
    }
  }
}

resource "aws_iam_policy" "ows_product_staging_s3_bucket_delete_policy" {
  name   = "S3-${var.environment}-ows-product-staging-bucket-delete"
  policy = data.aws_iam_policy_document.ows_product_staging_s3_bucket_delete_policy_document.json
}

resource "aws_iam_user_policy_attachment" "ows_product_staging_s3_bucket_rw_policy_apavlov" {
  user       = "apavlov"
  policy_arn = aws_iam_policy.ows_product_staging_s3_bucket_delete_policy.arn
}
