#!/bin/bash
set -euo pipefail

# Remote server configuration
REMOTE_USER="${REMOTE_USER:-root}"
REMOTE_HOST="${REMOTE_HOST:-}"
REMOTE_PATH="${REMOTE_PATH:-/opt/tm-back}"
SSH_KEY="${SSH_KEY:-}"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

usage() {
    echo "Usage: $0 --host <remote-host> [--user <user>] [--path <remote-path>] [--key <ssh-key>]"
    echo ""
    echo "Options:"
    echo "  --host   Remote server hostname or IP (required)"
    echo "  --user   Remote user (default: root)"
    echo "  --path   Remote destination path (default: /opt/tm-back)"
    echo "  --key    Path to SSH private key (optional)"
    echo ""
    echo "Environment variables REMOTE_USER, REMOTE_HOST, REMOTE_PATH, SSH_KEY can also be used."
    exit 1
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        --host) REMOTE_HOST="$2"; shift 2 ;;
        --user) REMOTE_USER="$2"; shift 2 ;;
        --path) REMOTE_PATH="$2"; shift 2 ;;
        --key)  SSH_KEY="$2"; shift 2 ;;
        -h|--help) usage ;;
        *) echo "Unknown option: $1"; usage ;;
    esac
done

if [[ -z "$REMOTE_HOST" ]]; then
    echo "Error: --host is required"
    usage
fi

SSH_CMD="ssh"
if [[ -n "$SSH_KEY" ]]; then
    SSH_CMD="ssh -i $SSH_KEY"
fi

echo "==> Uploading source code to ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"

# Create remote directory
${SSH_CMD} "${REMOTE_USER}@${REMOTE_HOST}" "mkdir -p ${REMOTE_PATH}"

# Rsync source code, excluding non-essential files
rsync -avz --progress \
    --exclude 'bin/' \
    --exclude 'artifacts/' \
    --exclude '.git/' \
    --exclude '.idea/' \
    --exclude '.vscode/' \
    --exclude '*.exe' \
    --exclude 'Dockerfile' \
    --exclude 'upload.sh' \
    ${SSH_KEY:+-e "ssh -i $SSH_KEY"} \
    "${SCRIPT_DIR}/" \
    "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/"

echo ""
echo "==> Upload complete to ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"
echo ""
echo "To build and run on the remote server:"
echo "  ${SSH_CMD} ${REMOTE_USER}@${REMOTE_HOST} 'cd ${REMOTE_PATH} && go build -o bin/web ./cmd/web'"
