variable "wait_on" {
  type        = any
  description = "Resources to wait on"
  default     = true
}

variable "server_ip" {
  type = string
}

variable "ssh_private_key_path" {
  type = string
}

variable "server_domain" {
  type = string
}

variable "db_password" {
  type      = string
  sensitive = true
}

variable "redis_password" {
  type      = string
  sensitive = true
}

variable "rabbitmq_password" {
  type      = string
  sensitive = true
}

variable "minio_access_key" {
  type      = string
  sensitive = true
}

variable "minio_secret_key" {
  type      = string
  sensitive = true
}

variable "secret_key" {
  type      = string
  sensitive = true
}

module "beat-worker" {
  source = "../../quadlet-app"
  wait_on = var.wait_on

  server_ip            = var.server_ip
  ssh_private_key_path = var.ssh_private_key_path

  restart_policy = "never"

  app_name = "plane-beat-worker"
  image    = "docker.io/makeplane/plane-backend:v1.0.0"
  volumes  = [
    "/opt/storage/data/plane-beat-worker:/var/www/plane:Z"
  ]
  environment = {
    # Database settings
    DATABASE_URL = "postgresql://plane:${var.db_password}@systemd-postgres:5432/plane"
    POSTGRES_USER = "plane"
    POSTGRES_PASSWORD = var.db_password
    POSTGRES_DB = "plane"

    # Redis settings
    REDIS_URL = "redis://:${var.redis_password}@systemd-valkey:6379/0"
    REDIS_HOST = "systemd-valkey"
    REDIS_PORT = "6379"

    # RabbitMQ settings
    RABBITMQ_URL = "amqp://plane:${var.rabbitmq_password}@systemd-rabbitmq:5672/plane"
    RABBITMQ_HOST = "systemd-rabbitmq"
    RABBITMQ_PORT = "5672"
    RABBITMQ_USER = "plane"
    RABBITMQ_PASSWORD = var.rabbitmq_password
    RABBITMQ_VHOST = "plane"

    # MinIO/S3 settings
    AWS_S3_ENDPOINT_URL = "http://systemd-minio:9000"
    AWS_ACCESS_KEY_ID = var.minio_access_key
    AWS_SECRET_ACCESS_KEY = var.minio_secret_key
    AWS_S3_BUCKET_NAME = "plane-uploads"
    AWS_REGION = "us-east-1"
    USE_MINIO = "1"

    # Application settings
    SECRET_KEY = var.secret_key
    DEBUG = "0"
    ENVIRONMENT = "production"
    
    # Web settings
    WEB_URL = "https://plane.${var.server_domain}"
    API_BASE_URL = "https://plane.${var.server_domain}/api"
    
    # File upload settings
    FILE_SIZE_LIMIT = "5242880"
    
    # Rate limiting
    API_KEY_RATE_LIMIT = "60/minute"
  }
  command = ["./bin/docker-entrypoint-beat.sh"]
}

output "installed" {
  value = true
  depends_on = [module.beat-worker.installed]
}