#!/bin/bash
# ============================================================
# mail.ru - Removal Script
# Generated by NginxForge
# ============================================================

set -euo pipefail

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }

[[ $EUID -ne 0 ]] && log_error "This script must be run as root"

DOMAIN="mail.ru"
ROOT_PATH="/var/www/mail.ru"
NGINX_CONF="/etc/nginx/sites-available/${DOMAIN}"
NGINX_ENABLED="/etc/nginx/sites-enabled/${DOMAIN}"

echo -e "${RED}============================================================${NC}"
echo -e "${RED} WARNING: This will remove ${DOMAIN}${NC}"
echo -e "${RED}============================================================${NC}"
echo ""
echo "This script will:"
echo "  - Remove nginx configuration"
echo "  - Revoke SSL certificate (if any)"
echo "  - Optionally delete site files"
echo ""
read -p "Are you sure? (y/N): " -n 1 -r
echo ""
[[ ! $REPLY =~ ^[Yy]$ ]] && exit 0

# ============================================================
# 1. Remove nginx configuration
# ============================================================
log_info "Removing nginx configuration..."

rm -f "${NGINX_ENABLED}"
rm -f "${NGINX_CONF}"

nginx -t && systemctl reload nginx
log_success "Nginx configuration removed"

# ============================================================
# 2. Remove SSL certificate
# ============================================================
log_info "Revoking SSL certificate..."

if [[ -d "/etc/letsencrypt/live/${DOMAIN}" ]]; then
    certbot delete --cert-name "${DOMAIN}" --non-interactive || true
    log_success "SSL certificate revoked"
else
    log_warn "No certificate found"
fi

# ============================================================
# 3. Remove site files (optional)
# ============================================================
echo ""
read -p "Delete site files at ${ROOT_PATH}? (y/N): " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
    if [[ -d "${ROOT_PATH}" ]]; then
        rm -rf "${ROOT_PATH}"
        log_success "Site files deleted"
    else
        log_warn "Directory not found"
    fi
else
    log_info "Site files preserved at ${ROOT_PATH}"
fi

# ============================================================
# 4. Remove log files (optional)
# ============================================================
read -p "Delete log files? (y/N): " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
    rm -f "/var/log/nginx/${DOMAIN}.access.log"
    rm -f "/var/log/nginx/${DOMAIN}.error.log"
    log_success "Log files deleted"
fi

# ============================================================
# Summary
# ============================================================
echo ""
echo -e "${GREEN}============================================================${NC}"
echo -e "${GREEN} ${DOMAIN} has been removed${NC}"
echo -e "${GREEN}============================================================${NC}"
echo ""