job "mothership-bot-[[BOT_ID]]" {
  # Region where the job should run
  region = "[[REGION]]"

  # Datacenter where the job should run
  datacenters = ["[[DATACENTER]]"]

  # Job type - service means it should run until explicitly stopped
  type = "service"

  # Update strategy
  update {
    max_parallel     = 1
    min_healthy_time = "10s"
    healthy_deadline = "3m"
    auto_revert      = true
  }

  # Task group - contains one or more tasks that should run together
  group "bot" {
    # Number of instances to run
    count = 1

    # Restart policy
    restart {
      attempts = 3
      interval = "5m"
      delay    = "15s"
      mode     = "fail"
    }

    # Network configuration
    network {
      # No specific ports needed for Telegram polling bots
      # If using webhooks, add port configuration here
      # port "http" {
      #   to = 3000
      # }
    }

    # The bot task
    task "bot" {
      # Driver to use - Docker
      driver = "docker"

      # Docker configuration
      config {
        image = "[[DOCKER_IMAGE]]"

        # Force pull to ensure latest image
        force_pull = true

        # Args can be overridden if needed
        # args = []
      }

      # Environment variables
      env {
        BOT_ID              = "[[BOT_ID]]"
        BOT_PATH            = "/app/bots/[[BOT_ID]]"
        NODE_ENV            = "production"
        PYTHONUNBUFFERED    = "1"
      }

      # Secrets from Vault (if USE_VAULT is enabled)
      [[VAULT_TEMPLATE]]

      # Direct token injection (if not using Vault)
      [[DIRECT_TOKEN_TEMPLATE]]

      # Resource limits
      resources {
        cpu    = [[CPU]]     # MHz
        memory = [[MEMORY]]  # MB
      }

      # Health check
      service {
        name = "mothership-bot-[[BOT_ID]]"

        tags = [
          "bot",
          "telegram",
          "mothership",
          "bot-id-[[BOT_ID]]"
        ]

        # For polling bots, we use a simple process check
        check {
          type     = "script"
          name     = "bot_running"
          command  = "/bin/sh"
          args     = ["-c", "pgrep -f '(node|python).*[[BOT_ID]]' || exit 1"]
          interval = "30s"
          timeout  = "5s"
        }
      }

      # Logs configuration
      logs {
        max_files     = 5
        max_file_size = 10 # MB
      }

      # Kill timeout - how long to wait for graceful shutdown
      kill_timeout = "30s"

      # Kill signal
      kill_signal = "SIGTERM"
    }

    # Ephemeral disk for logs and temporary files
    ephemeral_disk {
      size = 300 # MB
    }
  }

  # Metadata
  meta {
    bot_id      = "[[BOT_ID]]"
    bot_type    = "[[BOT_TYPE]]"
    deployed_at = "[[DEPLOYED_AT]]"
    deployed_by = "[[DEPLOYED_BY]]"
  }
}
