#!/usr/bin/env sh

set -eu

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
INSTALLER="$SCRIPT_DIR/install-agentflow.sh"

if [ ! -f "$INSTALLER" ]; then
  echo "installer script not found: $INSTALLER" >&2
  exit 1
fi

sha256_file() {
  if command -v sha256sum >/dev/null 2>&1; then
    sha256sum "$1" | awk '{print $1}'
    return
  fi
  if command -v shasum >/dev/null 2>&1; then
    shasum -a 256 "$1" | awk '{print $1}'
    return
  fi
  openssl dgst -sha256 "$1" | awk '{print $NF}'
}

detect_arch() {
  case "$(uname -m)" in
    x86_64|amd64) printf 'amd64\n' ;;
    arm64|aarch64) printf 'arm64\n' ;;
    *)
      echo "unsupported test architecture: $(uname -m)" >&2
      exit 1
      ;;
  esac
}

assert_contains() {
  needle=$1
  haystack_file=$2
  if ! grep -F "$needle" "$haystack_file" >/dev/null 2>&1; then
    echo "expected to find '$needle' in $haystack_file" >&2
    exit 1
  fi
}

create_test_binary() {
  output_path=$1
  version_text=$2
  cat >"$output_path" <<EOF
#!/usr/bin/env sh
if [ "\${1:-}" = "--version" ]; then
  printf '%s\n' '$version_text'
  exit 0
fi
printf '%s\n' '$version_text'
EOF
  chmod +x "$output_path"
}

run_install_case() {
  mode=$1
  root_mode=$2
  tmpdir=$(mktemp -d)
  trap 'rm -rf "$tmpdir"' EXIT INT TERM

  home_dir="$tmpdir/home"
  build_dir="$tmpdir/build"
  install_root="$tmpdir/install"
  mkdir -p "$home_dir" "$build_dir"

  profile_file="$home_dir/.profile"
  : >"$profile_file"

  arch=$(detect_arch)
  binary_path="$build_dir/agentflow"
  create_test_binary "$binary_path" '1.2.3'

  artifact_file="agentflow-linux-$arch"
  artifact_path="$build_dir/$artifact_file"
  if [ "$mode" = "tar" ]; then
    artifact_file="$artifact_file.tar.gz"
    artifact_path="$build_dir/$artifact_file"
    tar -C "$build_dir" -czf "$artifact_path" agentflow
  else
    cp "$binary_path" "$artifact_path"
  fi

  checksum=$(sha256_file "$artifact_path")
  version_json="$build_dir/version.json"
  cat >"$version_json" <<EOF
{
  "version": "1.2.3",
  "platforms": {
    "linux": {
      "$arch": {
        "file": "$artifact_file",
        "sha256": "$checksum",
        "url": "file://$artifact_path"
      }
    }
  }
}
EOF

  if [ "$root_mode" = "default" ]; then
    env HOME="$home_dir" SHELL="/bin/sh" sh "$INSTALLER" \
      --version-json-url "file://$version_json"
    install_root="$home_dir/.local/share/agentflow"
  else
    env HOME="$home_dir" SHELL="/bin/sh" sh "$INSTALLER" \
      --install-root "$install_root" \
      --version-json-url "file://$version_json"
  fi

  installed_bin="$install_root/bin/agentflow"
  if [ ! -x "$installed_bin" ]; then
    echo "expected installed binary at $installed_bin" >&2
    exit 1
  fi
  if [ "$("$installed_bin" --version)" != "1.2.3" ]; then
    echo "installed binary returned unexpected version" >&2
    exit 1
  fi
  assert_contains 'agentflow installer' "$profile_file"
  assert_contains 'AGENT_FLOW_DATA' "$profile_file"

  status_log="$tmpdir/check.log"
  if [ "$root_mode" = "default" ]; then
    env HOME="$home_dir" SHELL="/bin/sh" sh "$INSTALLER" \
      --check \
      --version-json-url "file://$version_json" >"$status_log"
  else
    env HOME="$home_dir" SHELL="/bin/sh" sh "$INSTALLER" \
      --check \
      --install-root "$install_root" \
      --version-json-url "file://$version_json" >"$status_log"
  fi
  assert_contains 'Installed: yes' "$status_log"
  assert_contains 'Remote version: 1.2.3' "$status_log"

  mkdir -p "$install_root/data"
  if [ "$root_mode" = "default" ]; then
    env HOME="$home_dir" SHELL="/bin/sh" sh "$INSTALLER" \
      --uninstall \
      --remove-data
  else
    env HOME="$home_dir" SHELL="/bin/sh" sh "$INSTALLER" \
      --uninstall \
      --remove-data \
      --install-root "$install_root"
  fi

  if [ -e "$installed_bin" ]; then
    echo "expected uninstall to remove binary" >&2
    exit 1
  fi
  if [ -e "$install_root/data" ]; then
    echo "expected uninstall to remove data directory" >&2
    exit 1
  fi

  rm -rf "$tmpdir"
  trap - EXIT INT TERM
}

run_platform_selection_case() {
  tmpdir=$(mktemp -d)
  trap 'rm -rf "$tmpdir"' EXIT INT TERM

  home_dir="$tmpdir/home"
  build_dir="$tmpdir/build"
  install_root="$tmpdir/install"
  mkdir -p "$home_dir" "$build_dir"
  : >"$home_dir/.profile"

  create_test_binary "$build_dir/agentflow-darwin" 'darwin-1.2.3'
  create_test_binary "$build_dir/agentflow-linux" 'linux-1.2.3'

  darwin_sha=$(sha256_file "$build_dir/agentflow-darwin")
  linux_sha=$(sha256_file "$build_dir/agentflow-linux")

  cat >"$build_dir/version.json" <<EOF
{
  "version": "1.2.3",
  "platforms": {
    "darwin": {
      "amd64": {
        "file": "agentflow-darwin-amd64",
        "sha256": "$darwin_sha",
        "url": "file://$build_dir/agentflow-darwin"
      }
    },
    "linux": {
      "amd64": {
        "file": "agentflow-linux-amd64",
        "sha256": "$linux_sha",
        "url": "file://$build_dir/agentflow-linux"
      }
    }
  }
}
EOF

  env \
    HOME="$home_dir" \
    SHELL="/bin/sh" \
    AGENTFLOW_INSTALLER_OS="darwin" \
    AGENTFLOW_INSTALLER_ARCH="amd64" \
    sh "$INSTALLER" \
      --install-root "$install_root" \
      --version-json-url "file://$build_dir/version.json"

  installed_bin="$install_root/bin/agentflow"
  if [ "$("$installed_bin" --version)" != "darwin-1.2.3" ]; then
    echo "expected darwin artifact to be installed" >&2
    exit 1
  fi

  rm -rf "$tmpdir"
  trap - EXIT INT TERM
}

run_install_case raw custom
run_install_case tar custom
run_install_case raw default
run_platform_selection_case

echo "installer tests passed"
