module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-store/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "ows_store_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"

  environment_name = var.environment
  service_name     = var.service_name
}

data "aws_caller_identity" "current" {}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=3.1.0"

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

module "ows_store_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.1"

  providers = {
    aws.dns = aws.networking
  }

  environment        = var.environment
  service_name       = var.service_name
  aws_region         = var.aws_region
  application_family = var.application_family
  commit_sha         = "latest"
  container_port     = "8080"
  task_type          = "web_service"
  desired_task_count = 2
  minimum_capacity   = 2
  maximum_capacity   = 4
  task_cpu           = 2048
  task_memory        = 4096
  route53_zone_id    = data.aws_route53_zone.route53_zone.zone_id

  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  blocking_waf_enabled          = true

  iam_managed_policy_attachments = [
    module.ows_store_owsrequest.policy_arn_output
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_LEVEL = var.logger_level
    },
    {
      MYSQL_PORT = "3306"
    },
    {
      MYSQL_HOST = var.MYSQL_HOST
    },
    {
      MYSQL_DATABASE = var.MYSQL_DATABASE
    },
    {
      MYSQL_USER = var.MYSQL_USER
    },
    {
      SENTRY_DSN = module.sentry_ows_store.sentry_key_dsn_public_output
    },
  ]
}

module "ows_store_fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  critical_number_5xx               = var.critical_number_5xx
  critical_recovery_number_5xx      = var.critical_recovery_number_5xx
  warning_number_5xx                = var.warning_number_5xx
  warning_recovery_number_5xx       = var.warning_recovery_number_5xx
  service_cpu_time_window           = var.cpu_time_window
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
  additional_tags                   = { service_type : "critical" }
  teams                             = [var.application_family]
  service_4xx_monitor_enabled       = false
  dependent_applications            = var.dependent_applications
}

module "sentry_ows_store" {
  source = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  teams              = [var.environment]
  application_family = var.application_family
}

module "ows_store_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"

  application_family = var.application_family
  environment        = var.environment
  service_name       = var.service_name
  secret_name        = var.secret_name
}
