#!/usr/bin/env bash
set -euo pipefail

readonly SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
readonly REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
readonly STACK_DIR="$REPO_ROOT/infra/aws-tokyo"
readonly RELEASE_DIR="$STACK_DIR/.terraform-build"
readonly RELEASE_ARCHIVE_PATH="$RELEASE_DIR/agentteam-release.tar.gz"
readonly KNOWN_HOSTS_PATH="$RELEASE_DIR/known_hosts"
readonly DEFAULT_ENV_FILE_PATH="$STACK_DIR/agentteam.env.production"

source "$SCRIPT_DIR/_lib.sh"

usage() {
  cat <<'EOF' >&2
Usage:
  scripts/release/deploy-release.sh

Environment variables:
  AGENTTEAM_ENV_FILE_PATH         (default: infra/aws-tokyo/agentteam.env.production)
  AGENTTEAM_PUBLIC_IP            (required unless Terraform state exists)
  AGENTTEAM_SSH_PRIVATE_KEY_PATH (default: ~/.ssh/id_rsa)
  AGENTTEAM_SSH_PORT             (default: 2222)
  AGENTTEAM_SSH_USER             (default: ubuntu)

This script performs an app-only deployment:
  0) Preflight: wait for SSH connectivity (port 2222)
  1) Build backend + web release tarball locally
  2) Sync local env file to the server (without restarting)
  3) Upload and activate release atomically on the server

During activation the server runs an explicit `agentteam-schema init` step before
restarting `agentteam-server`.

It intentionally does NOT run `terraform apply`.
EOF
}

public_ip="$(agentteam_resolve_public_ip "$STACK_DIR")"
if [[ -z "$public_ip" ]]; then
  usage
  echo "" >&2
  echo "AGENTTEAM_PUBLIC_IP is required (or run with Terraform + local state for infra/aws-tokyo)." >&2
  exit 1
fi

ssh_private_key_path="${AGENTTEAM_SSH_PRIVATE_KEY_PATH:-$HOME/.ssh/id_rsa}"
ssh_port="${AGENTTEAM_SSH_PORT:-2222}"
ssh_user="${AGENTTEAM_SSH_USER:-ubuntu}"
env_file_path="${AGENTTEAM_ENV_FILE_PATH:-$DEFAULT_ENV_FILE_PATH}"

if [[ ! -f "$ssh_private_key_path" ]]; then
  echo "SSH private key not found: $ssh_private_key_path" >&2
  exit 1
fi
if [[ ! -f "$env_file_path" ]]; then
  echo "env file not found: $env_file_path" >&2
  echo 'hint: run `terraform apply` once (it will bootstrap infra/aws-tokyo/agentteam.env.production), or run `bash scripts/release/pull-env.sh` to pull from an existing instance.' >&2
  exit 1
fi

require_command bash
require_command ssh

mkdir -p "$RELEASE_DIR"

agentteam_prepare_known_hosts "$KNOWN_HOSTS_PATH"
agentteam_build_ssh_options "$ssh_private_key_path" "$ssh_port" "$KNOWN_HOSTS_PATH"
agentteam_wait_for_ssh "$ssh_user" "$public_ip" "$ssh_port"

AGENTTEAM_RELEASE_ARCHIVE_PATH="$RELEASE_ARCHIVE_PATH" bash "$SCRIPT_DIR/build-release.sh"

AGENTTEAM_ENV_FILE_PATH="$env_file_path" \
  AGENTTEAM_PUBLIC_IP="$public_ip" \
  AGENTTEAM_SSH_PRIVATE_KEY_PATH="$ssh_private_key_path" \
  AGENTTEAM_SSH_PORT="$ssh_port" \
  AGENTTEAM_SSH_USER="$ssh_user" \
  AGENTTEAM_SSH_KNOWN_HOSTS_PATH="$KNOWN_HOSTS_PATH" \
  AGENTTEAM_SYNC_ENV_RESTART=0 \
  bash "$SCRIPT_DIR/sync-env.sh"

AGENTTEAM_PUBLIC_IP="$public_ip" \
  AGENTTEAM_RELEASE_ARCHIVE_PATH="$RELEASE_ARCHIVE_PATH" \
  AGENTTEAM_SSH_PRIVATE_KEY_PATH="$ssh_private_key_path" \
  AGENTTEAM_SSH_PORT="$ssh_port" \
  AGENTTEAM_SSH_USER="$ssh_user" \
  AGENTTEAM_SSH_KNOWN_HOSTS_PATH="$KNOWN_HOSTS_PATH" \
  bash "$SCRIPT_DIR/upload-release.sh"

echo "deployed release to $ssh_user@$public_ip:$ssh_port"
