#!/bin/bash
# ==========================================
#  PREMIUM DELETE VMESS (AUTO RELOAD)
#  MODIFIED BY GEMINI AI
# ==========================================

# --- CONFIG COLORS & SYMBOLS ---
R='\033[0m'             # Reset
P='\033[1;37m'          # Putih
M='\033[1;31m'          # Merah
H='\033[1;32m'          # Hijau
K='\033[1;33m'          # Kuning
C='\033[1;36m'          # Cyan
Kedip='\033[5m'         # Efek Kedip

# Background Blocks
BG_M='\033[41m\033[1;37m' # Merah Block
BG_G='\033[42m\033[1;37m' # Hijau Block
BG_K='\033[43m\033[1;30m' # Kuning Block

# Symbols & Lines
ki="${C}│${R}"
ka="${C}│${R}"
Garis="${C}────────────────────────────────────────────${R}"
Atas="${C}┌$Garis┐${R}"
Tengah="${C}├$Garis┤${R}"
Bawah="${C}└$Garis┘${R}"

# --- SYSTEM CHECK ---
dateFromServer=$(curl -v --insecure --silent https://google.com/ 2>&1 | grep Date | sed -e 's/< Date: //')
biji=`date +"%Y-%m-%d" -d "$dateFromServer"`

# ==========================================
#  LOOPING UTAMA (AGAR TIDAK KEMBALI KE MENU)
# ==========================================
while true; do
    clear
    echo -e ""
    echo -e "         ${Kedip}${K}🗑️  DELETE VMESS ACCOUNT 🗑️${R}"
    echo -e ""

    # --- 1. AMBIL DATA USER KE ARRAY ---
    mapfile -t user_list < <(grep -E "^### " "/etc/xray/config.json" | cut -d ' ' -f 2 | sort | uniq)
    total_client=${#user_list[@]}

    # --- CEK JIKA KOSONG ---
    if [[ ${total_client} == '0' ]]; then
        echo -e "$Atas"
        echo -e "$ki${BG_M}              NO CLIENT DATA                ${R}$ka"
        echo -e "$Tengah"
        echo -e "$ki ${M}You have no existing clients!            ${R}$ka"
        echo -e "$Bawah"
        echo ""
        read -n 1 -s -r -p " Press any key to back on menu"
        menu
        break
    fi

    # --- 2. TAMPILKAN LIST DENGAN NOMOR ---
    echo -e "$Atas"
    echo -e "$ki${BG_K}              LIST VMESS USER               ${R}$ka"
    echo -e "$Tengah"
    echo -e "$ki  ${P}NO  USER                  EXPIRED        ${R}$ka"
    echo -e "$Tengah"

    no=1
    for user in "${user_list[@]}"; do
        exp=$(grep -wE "^### $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)
        printf "$ki  ${C}%-4s${P}%-20s${K}%-15s${R}$ka\n" "$no)" "$user" "$exp"
        ((no++))
    done
    
    # --- TAMBAHAN MENU KEMBALI ---
    echo -e "$Tengah"
    echo -e "$ki  ${M}0)  BACK TO MAIN MENU                    ${R}$ka"
    echo -e "$Bawah"
    echo -e ""
    echo -e " ${P}Type Number (1-$total_client) to delete, or (0) to exit${R}"
    echo -e " ${Garis}"

    # --- 3. INPUT ---
    echo -ne " ${C}➤ Input Number / Username : ${P}"
    read selection

    # --- 4. LOGIKA INPUT ---
    
    # A. Jika Input Kosong -> Refresh Loop
    if [ -z "$selection" ]; then
        continue
    fi

    # B. Jika Input '0' -> Kembali ke Menu Utama
    if [[ "$selection" == "0" ]]; then
        menu
        break
    fi

    # C. Jika Input Angka -> Konversi ke Username
    if [[ $selection =~ ^[0-9]+$ ]]; then
        if [[ $selection -ge 1 && $selection -le $total_client ]]; then
            user="${user_list[$((selection-1))]}"
        else
            echo -e " ${M}[!] Error: Number out of range!${R}"
            sleep 1
            continue
        fi
    else
        # D. Jika Input Username Manual
        user="$selection"
        if ! grep -qws "^### $user" "/etc/xray/config.json"; then
            echo -e " ${M}[!] Error: Username not found!${R}"
            sleep 1
            continue
        fi
    fi

    # --- 5. PROSES DELETE ---
    exp=$(grep -wE "^### $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)

    sed -i "/^### $user $exp/,/^},{/d" /etc/xray/config.json
    sed -i "/^### $user $exp/,/^},{/d" /etc/vmess/.vmess.db
    rm -rf /etc/vmess/$user
    rm -rf /etc/hokage/limit/vmess/ip/$user
    systemctl restart xray > /dev/null 2>&1

    # --- TAMPILAN SUKSES ---
    clear
    echo -e ""
    echo -e "         ${Kedip}${K}✅ ACCOUNT DELETED SUCCESSFULLY ✅${R}"
    echo -e ""
    echo -e "$Atas"
    echo -e "$ki${BG_G}              DELETED DETAILS               ${R}$ka"
    echo -e "$Tengah"
    printf "$ki  ${P}%-14s ${C}: ${M}%-24s${R}$ka\n" "Client Name" "$user"
    printf "$ki  ${P}%-14s ${C}: ${K}%-24s${R}$ka\n" "Expired On" "$exp"
    printf "$ki  ${P}%-14s ${C}: ${P}%-24s${R}$ka\n" "Deleted On" "$biji"
    echo -e "$Bawah"
    echo -e ""
    
    # --- PAUSE SEBELUM RELOAD ---
    echo -e "${C} Account deleted. Returning to list...${R}"
    read -n 1 -s -r -p " Press any key to continue"
    # Setelah tekan tombol, script akan kembali ke awal 'while true' (Refresh List)

done