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

# ============================================
# Joplin Server 卸载脚本
# 停止容器 → 备份数据 → 清理容器/镜像/配置/数据
# ============================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

log()   { echo -e "${GREEN}[INFO]${NC} $*"; }
warn()  { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }

# ===== 检查 root =====
if [ "$(id -u)" -ne 0 ]; then
    error "请使用 root 用户运行: sudo bash uninstall.sh"
    exit 1
fi

# ===== 加载配置 =====
if [ -f .env ]; then
    sed -i 's/\r$//' .env
    set -a
    source .env
    set +a
fi

JOPLIN_DB_DIR="${JOPLIN_DB_DIR:-/data/joplin/db}"
BACKUP_DIR="${BACKUP_DIR:-/var/backups/joplin}"
JOPLIN_DOMAIN="${JOPLIN_DOMAIN:-}"

# ===== 确认操作 =====
echo ""
echo -e "${RED}╔══════════════════════════════════════════════════╗${NC}"
echo -e "${RED}║      ⚠  即将卸载 Joplin Server 及所有数据  ⚠     ║${NC}"
echo -e "${RED}╚══════════════════════════════════════════════════╝${NC}"
echo ""
echo "将执行以下操作："
echo "  1. 停止并删除 Joplin Server + PostgreSQL 容器"
echo "  2. 删除 Docker 镜像"
echo "  3. 删除 Nginx 站点配置"
echo "  4. 删除 SSL 证书"
echo "  5. 清理 Certbot 自动续期定时任务"
echo ""
echo "涉及的数据目录："
echo "  数据库数据: ${JOPLIN_DB_DIR}"
echo "  备份目录:   ${BACKUP_DIR}"
echo "  部署目录:   ${SCRIPT_DIR}"
echo ""
echo -e "${YELLOW}备份目录将保留，不会被删除。${NC}"
echo ""
read -r -p "确定要继续卸载吗？输入 YES 确认: " confirm
if [ "$confirm" != "YES" ]; then
    log "已取消卸载"
    exit 0
fi

# ===== 卸载前备份 =====
echo ""
read -r -p "是否在卸载前执行一次备份？(y/N): " do_backup
if [[ "$do_backup" =~ ^[Yy]$ ]]; then
    if [ -f backup.sh ]; then
        log "正在执行备份..."
        bash backup.sh
        log "备份完成"
    else
        warn "backup.sh 不存在，跳过备份"
    fi
fi

# ===== 1. 停止并删除容器 =====
echo ""
log "正在停止并删除容器..."
if docker compose ps --quiet 2>/dev/null | grep -q .; then
    docker compose down -v
    log "容器已停止并删除"
else
    log "没有运行中的容器"
fi

# ===== 2. 删除 Docker 镜像 =====
log "正在删除 Docker 镜像..."
JOPLIN_IMAGE="${JOPLIN_IMAGE:-joplin/server:latest}"
POSTGRES_IMAGE="${POSTGRES_IMAGE:-postgres:16-alpine}"
docker image rm "$JOPLIN_IMAGE" 2>/dev/null && log "已删除镜像: $JOPLIN_IMAGE" || true
docker image rm "$POSTGRES_IMAGE" 2>/dev/null && log "已删除镜像: $POSTGRES_IMAGE" || true

# ===== 3. 删除 Nginx 配置 =====
log "正在清理 Nginx 配置..."
rm -f /etc/nginx/sites-enabled/joplin
rm -f /etc/nginx/sites-available/joplin
if command -v nginx &>/dev/null && nginx -t 2>/dev/null; then
    systemctl reload nginx 2>/dev/null || true
    log "Nginx 已重载"
fi

# ===== 4. 删除 SSL 证书 =====
if [ -n "$JOPLIN_DOMAIN" ] && [ -d "/etc/letsencrypt/live/${JOPLIN_DOMAIN}" ]; then
    log "正在删除 SSL 证书: ${JOPLIN_DOMAIN}..."
    certbot delete --cert-name "${JOPLIN_DOMAIN}" --non-interactive 2>/dev/null || true
    log "SSL 证书已删除"
fi

# ===== 5. 清理 Certbot 定时任务 =====
remaining_certs=$(certbot certificates 2>/dev/null | grep -c "Certificate Name" || true)
if [ "$remaining_certs" -eq 0 ]; then
    crontab -l 2>/dev/null | grep -v "certbot renew" | crontab - 2>/dev/null || true
    log "已移除 Certbot 自动续期定时任务（无剩余证书）"
else
    log "保留 Certbot 定时任务（还有 ${remaining_certs} 个其他证书）"
fi

# ===== 6. 删除数据目录 =====
echo ""
echo -e "${RED}以下目录将被永久删除：${NC}"
echo "  ${JOPLIN_DB_DIR}"
echo ""
echo -e "${YELLOW}备份目录 ${BACKUP_DIR} 不会被删除。${NC}"
echo ""
read -r -p "确认删除数据目录？输入 DELETE 确认: " confirm_delete
if [ "$confirm_delete" = "DELETE" ]; then
    rm -rf "$JOPLIN_DB_DIR"
    # 清理父目录（如果为空）
    rmdir "$(dirname "$JOPLIN_DB_DIR")" 2>/dev/null || true
    log "已删除: ${JOPLIN_DB_DIR}"
else
    warn "跳过数据目录删除"
fi

# ===== 7. 删除部署目录 =====
echo ""
read -r -p "是否删除部署目录 ${SCRIPT_DIR}？(y/N): " del_deploy
if [[ "$del_deploy" =~ ^[Yy]$ ]]; then
    cd /opt
    rm -rf "$SCRIPT_DIR"
    log "已删除部署目录"
else
    warn "保留部署目录: ${SCRIPT_DIR}"
fi

# ===== 完成 =====
echo ""
log "Joplin Server 卸载完成"
echo ""
echo "保留的内容："
echo "  备份目录: ${BACKUP_DIR}"
[ "$confirm_delete" != "DELETE" ] && echo "  数据目录: ${JOPLIN_DB_DIR}"
[[ ! "$del_deploy" =~ ^[Yy]$ ]] && echo "  部署目录: ${SCRIPT_DIR}"
echo ""
echo "如需恢复，请参考 README.md 中的「恢复备份」章节。"
