# General settings.
variable "aws_region" {
  description = "AWS region."
  type        = string
  default     = "us-east-1"
}

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

variable "service_name" {
  description = "Name of service."
  type        = string
}

variable "application_family" {
  description = "Application family to which this service belongs."
  type        = string
}

# Airflow settings.
variable "airflow_version" {
  description = "Airflow engine version. Available values: 1.10.12, 2.0.2."
  type        = string
  default     = "1.10.12"
}

variable "airflow_environment_class" {
  description = "Airflow instance class. Available values: mw1.small, mw1.medium, mw1.large."
  type        = string
  default     = "mw1.small"
}

variable "airflow_min_workers" {
  description = "Airflow's minimum number of workers that you want to run in your environment."
  type        = number
  default     = 1
}

variable "airflow_max_workers" {
  description = "Airflow's maximum number of workers that can be automatically scaled up. The available range is from 1 to 25."
  type        = number
  default     = 10
}

variable "airflow_log_enabled" {
  description = "Enables or disables the collection of logs."
  type        = bool
  default     = true
}

variable "airflow_log_level" {
  description = "Airflow logging level. Available values: CRITICAL, ERROR, WARNING, INFO, DEBUG."
  type        = string
  default     = "INFO"
}

variable "airflow_configuration_options" {
  description = "Airflow configuration options. Reference: https://docs.aws.amazon.com/mwaa/latest/userguide/configuring-env-variables.html#configuring-env-variables-reference"
  type        = map(string)
  default     = {}
}

variable "airflow_external_policies_to_attach" {
  description = "Existing IAM policies that will be attached to the Airflow execution role, to provide the environment with additional permissions."
  type        = list(string)
  default     = []
}

variable "airflow_create_iam_policies_for_roles_web_auth" {
  description = "Create IAM policies to access Airflow web UI using the internal Airflow roles. Available values: Admin, Op, User, Viewer, Public."
  type        = list(string)
  default     = ["Admin", "User"]
}

# S3 settings.
variable "bucket_name" {
  description = "Name of S3 bucket to contain source code."
  type        = string
}

variable "bucket_dags_path" {
  description = "Path to the DAGs folder on the S3 bucket."
  type        = string
  default     = "dags"
}

variable "bucket_plugins_path" {
  description = "Path to the plugins.zip file on the S3 bucket."
  type        = string
  default     = "plugins.zip"
}

variable "bucket_requirements_path" {
  description = "Path to the requirements.txt file on the S3 bucket."
  type        = string
  default     = "requirements.txt"
}

# CloudWatch settings.
variable "log_retention_in_days" {
  description = "Amount of days that we store logs for in CloudWatch."
  type        = number
  default     = 365
}

variable "log_forward_to_datadog_enabled" {
  description = "Enables or disables the shipping of CloudWatch logs to DataDog."
  type        = bool
  default     = true
}

# VPC settings.
variable "vpc_id" {
  description = "VPC ID for Airflow to be placed in. Dev VPC by default."
  type        = string
  default     = "vpc-34dbfd51"
}

variable "vpc_subnet_ids" {
  description = "List of subnet IDs for Airflow to use. Dev subnets by default."
  type        = list(string)
  default     = ["subnet-b649dfef", "subnet-44c19a21"]
}

variable "https_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow HTTPS access to Airflow."
  type        = list(string)
  default     = []
}

variable "https_listener_allow_prefix_lists" {
  description = "Prefix lists from which to allow HTTPS access to Airflow."
  type        = list(string)
  default     = ["vpn-ny-users"]
}

# DNS settings.
variable "override_route53_zone_id" {
  description = "AWS Route 53 zone id to override the default zone. This should be used with the DNS provider. If not provided, defaults to dev.theorchard.io for dev environment and theorchard.io for all other environments."
  type        = string
  default     = null
}

variable "route53_record_creation_enabled" {
  description = "Whether to create Route53 records. Set to false to disable Route53 record creation."
  type        = bool
  default     = true
}

variable "weekly_maintenance_window_start" {
  description = "Weekly maintenance window start time in UTC. Format: DAY:HH:MM."
  type        = string
  default     = "SAT:08:00"
}

# Locals.
locals {
  environment_name = "${var.environment}-${var.service_name}"

  cloudwatch_log_group_prefix   = "airflow-${var.environment}-${var.service_name}"
  cloudwatch_log_group_suffixes = ["DAGProcessing", "Scheduler", "Task", "WebServer", "Worker"]
  datadog_lambda_arn            = "arn:aws:lambda:${var.aws_region}:${data.aws_caller_identity.current.account_id}:function:DatadogLambdaFunction"

  # Using a simpler approach to avoid escape sequence issues
  bucket_dummy_dag_path = "${var.bucket_dags_path}/dummy_dag.py"

  route53_zone_id = var.override_route53_zone_id != null ? var.override_route53_zone_id : (var.environment == "dev" ? "Z21XEY26C989RH" : "Z0183645HDT0XCWHLW7S")

  tags = {
    environment        = var.environment
    service_name       = var.service_name
    terraformed        = true
    application_family = var.application_family
  }
}
