variable "region" {
  type        = string
  description = "AWS region to deploy into."
  default     = "us-west-2"
}

variable "tfstate_passphrase" {
  type        = string
  description = <<EOT
Passphrase for OpenTofu state encryption (PBKDF2 → AES-GCM). Leave empty
to leave state unencrypted (the default). To enable, copy
`encryption.tofu.example` to `encryption.tofu`
in the working dir, set this variable (or `TF_VAR_tfstate_passphrase`)
to at least 16 characters, and run `tofu init -reconfigure`. Minimum
length is enforced by the pbkdf2 key_provider.
EOT
  default     = ""
  sensitive   = true
}

variable "aws_profile" {
  type        = string
  description = "Local AWS CLI profile Terraform should use. Must already be configured via `aws configure` or `aws sso login`."
  default     = "xylabs-datalake"
}

variable "environment" {
  type        = string
  description = "Environment name (staging, prod, etc.). Goes into resource tags + names."
  default     = "staging"
}

variable "project" {
  type        = string
  description = "Project name; goes into resource tags."
  default     = "aries-datalake"
}

variable "vpc_cidr" {
  type        = string
  description = "CIDR block for the dedicated VPC. Must not overlap any existing VPC you intend to peer with."
  default     = "10.42.0.0/16"
}

variable "availability_zone_count" {
  type        = number
  description = "Number of AZs to span (>= 2 required for the ALB)."
  default     = 2
}

variable "domain_name" {
  type        = string
  description = "Apex domain that hosts api.<domain> and data.<domain>. Leave empty to skip Route53 + ACM and expose the ALB DNS name directly (useful for first-bootstrap)."
  default     = ""
}

variable "create_route53_zone" {
  type        = bool
  description = "When true, Terraform creates the Route53 hosted zone for `domain_name`. When false, it looks up an existing zone by `domain_name`."
  default     = false
}

variable "db_instance_class" {
  type        = string
  description = "RDS instance class. Default is the cheapest reasonable option for staging."
  default     = "db.t4g.micro"
}

variable "db_engine_version" {
  type        = string
  description = "PostgreSQL version or version prefix. OpenTofu selects the latest regionally available match."
  default     = "16"
}

variable "db_allocated_storage" {
  type        = number
  description = "Initial RDS storage in GB."
  default     = 20
}

variable "control_image" {
  type        = string
  description = "Full ECR image URI for the control plane (including tag). Set after first `docker push` to the ECR repo this stack creates."
  default     = ""
}

variable "plane_image" {
  type        = string
  description = "Full ECR image URI for the data plane (including tag). Set after first `docker push` to the ECR repo this stack creates."
  default     = ""
}

variable "control_desired_count" {
  type        = number
  description = "Number of control-plane Fargate tasks."
  default     = 1
}

variable "plane_desired_count" {
  type        = number
  description = "Number of shared data-plane Fargate tasks (small/medium tiers)."
  default     = 1
}

variable "control_task_cpu" {
  type        = number
  description = "Fargate CPU units for the control plane (256 = 0.25 vCPU)."
  default     = 256
}

variable "control_task_memory" {
  type        = number
  description = "Fargate memory MB for the control plane."
  default     = 512
}

variable "plane_task_cpu" {
  type        = number
  description = "Fargate CPU units for the data plane."
  default     = 512
}

variable "plane_task_memory" {
  type        = number
  description = "Fargate memory MB for the data plane."
  default     = 1024
}

variable "scope_tags" {
  type        = map(string)
  description = "Tags applied to every resource via the AWS provider's default_tags. Populated by the `aries compute add --infra ...` flow with the infra-uid + managed-by tag set; left empty when the legacy `aries compute aws ...` commands invoke the module."
  default     = {}
}

variable "create_ecr_repos" {
  type        = bool
  description = "When true (default), the module provisions its own ECR repositories for the control + plane images. The infra-aware `aries compute add --kind serverless` flow passes false because ECR repos are managed by `aries infra images` at the infrastructure level instead. When false, `control_image` and `plane_image` must be set to full URIs."
  default     = true
}

variable "enable_blobs" {
  type        = bool
  description = "When true, provision the dedicated blob bucket, its plane IAM policy and audit secret, raise the ALB idle timeout to 120 s, and run the data plane with BLOB_STORE=s3. Default false leaves the stack unchanged. Disable only once the bucket holds no objects under objects/ (the bucket is never force-destroyed)."
  default     = false
}

variable "enable_s3_gateway_endpoint" {
  type        = bool
  description = "When true, add an S3 gateway VPC endpoint on the private route table so S3 traffic (blob bytes, payloads and ECR image layers) bypasses the NAT gateway. Uses the default full-access endpoint policy. Independent of enable_blobs."
  default     = false
}

variable "blob_max_bytes" {
  type        = number
  description = "Deployment per-blob size cap passed to the data plane as BLOB_MAX_BYTES. Only used when enable_blobs is true. Must not exceed the 1 GiB contract ceiling."
  default     = 104857600

  validation {
    condition     = var.blob_max_bytes >= 1 && var.blob_max_bytes <= 1073741824 && floor(var.blob_max_bytes) == var.blob_max_bytes
    error_message = "blob_max_bytes must be a whole number of bytes between 1 and 1073741824 (1 GiB)."
  }
}

locals {
  name_prefix = "${var.project}-${var.environment}"

  common_tags = merge({
    Project     = var.project
    Environment = var.environment
    ManagedBy   = "terraform"
  }, var.scope_tags)

  has_domain = length(var.domain_name) > 0
}
