#!/bin/bash
# ══════════════════════════════════════════════════════════════════
#  WhatsApp Bridge — cPanel Setup Script
#  Run once: bash setup.sh
# ══════════════════════════════════════════════════════════════════

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

echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${CYAN}  WhatsApp Gateway Setup for cPanel Shared Hosting${NC}"
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"

# Detect Node
NODE_BIN=$(which node 2>/dev/null || which node18 2>/dev/null || which node20 2>/dev/null || echo "")
if [ -z "$NODE_BIN" ]; then
    echo -e "${RED}ERROR: Node.js not found. Enable it in cPanel → Software → NodeJS.${NC}"
    exit 1
fi
NODE_VER=$($NODE_BIN --version)
echo -e "${GREEN}✓ Node.js found: $NODE_BIN ($NODE_VER)${NC}"

# Detect npm
NPM_BIN=$(which npm 2>/dev/null || echo "")
if [ -z "$NPM_BIN" ]; then
    echo -e "${RED}ERROR: npm not found.${NC}"; exit 1
fi
echo -e "${GREEN}✓ npm found: $NPM_BIN${NC}"

# Create dirs
mkdir -p data/auth logs
chmod 755 data logs
echo -e "${GREEN}✓ Directories created${NC}"

# Install Node deps
echo -e "${CYAN}Installing Node.js dependencies (this may take 1-2 mins)...${NC}"
cd bridge
$NPM_BIN install --production --no-audit --no-fund
cd ..
echo -e "${GREEN}✓ npm packages installed${NC}"

# Detect PHP
PHP_BIN=$(which php 2>/dev/null || which php8 2>/dev/null || which php81 2>/dev/null || echo "php")
echo -e "${GREEN}✓ PHP: $($PHP_BIN --version | head -1)${NC}"

# Make keepalive executable
chmod +x bridge/keepalive.php
echo -e "${GREEN}✓ keepalive.php made executable${NC}"

# Write .env with detected paths
ABS_PATH=$(pwd)
cat > .env <<ENV
NODE_BIN=$NODE_BIN
PHP_BIN=$PHP_BIN
BASE_DIR=$ABS_PATH
BRIDGE_PORT=3001
WA_ADMIN_TOKEN=CHANGE_THIS_SECRET_$(openssl rand -hex 8)
ENV
echo -e "${GREEN}✓ .env written${NC}"

# Write .htaccess to protect data/ dir
cat > data/.htaccess <<'HTACCESS'
Order Deny,Allow
Deny from all
HTACCESS
echo -e "${GREEN}✓ data/.htaccess (protection) written${NC}"

cat > logs/.htaccess <<'HTACCESS'
Order Deny,Allow
Deny from all
HTACCESS

# Print next steps
echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}  SETUP COMPLETE! Next steps:${NC}"
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "${YELLOW}1. Edit these files and change your tokens/passwords:${NC}"
echo "   → dashboard/index.php : Change \$ADMIN_PASS"
echo "   → api/whatsapp.php    : Change WA_ADMIN_TOKEN (match .env)"
echo "   → .env                : Review WA_ADMIN_TOKEN"
echo ""
echo -e "${YELLOW}2. Add Cron Job in cPanel (Cron Jobs section):${NC}"
echo "   Command: $PHP_BIN $ABS_PATH/bridge/keepalive.php >> $ABS_PATH/logs/cron.log 2>&1"
echo "   Schedule: Every 1 minute  (* * * * *)"
echo ""
echo -e "${YELLOW}3. Start the bridge manually first time:${NC}"
echo "   nohup $NODE_BIN $ABS_PATH/bridge/index.js > $ABS_PATH/logs/bridge_out.log 2>&1 &"
echo ""
echo -e "${YELLOW}4. Open the dashboard:${NC}"
echo "   https://yourdomain.com/whatsapp/dashboard/"
echo ""
echo -e "${YELLOW}5. Scan the QR code with WhatsApp on your phone:${NC}"
echo "   WhatsApp → ⋮ → Linked Devices → Link a Device"
echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
