#!/bin/bash
# ============================================================
# LIS Portal — cPanel SSH Setup Script
# Run this from the lis-portal/ root directory:
#   bash deploy_cpanel.sh
# ============================================================

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

echo -e "${GREEN}=== LIS Portal cPanel Deployment ===${NC}"

# ── Check PHP version ────────────────────────────────────────
PHP_VER=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;")
echo -e "PHP version: ${GREEN}$PHP_VER${NC}"
if php -r "exit(version_compare(PHP_VERSION,'8.2','<') ? 1 : 0);"; then
    echo -e "${GREEN}✓ PHP 8.2+ OK${NC}"
else
    echo -e "${RED}✗ PHP 8.2+ required. Set it in cPanel → MultiPHP Manager.${NC}"; exit 1
fi

# ── Composer install ─────────────────────────────────────────
echo -e "\n${YELLOW}Installing Composer dependencies...${NC}"
if command -v composer &>/dev/null; then
    composer install --no-dev --optimize-autoloader
else
    # Try common cPanel composer paths
    for BIN in /usr/local/bin/composer /opt/cpanel/composer/bin/composer; do
        [ -f "$BIN" ] && { $BIN install --no-dev --optimize-autoloader; break; }
    done
fi
echo -e "${GREEN}✓ Dependencies installed${NC}"

# ── .env setup ───────────────────────────────────────────────
if [ ! -f .env ]; then
    cp .env.example .env
    echo -e "${YELLOW}⚠ .env created from .env.example — edit DB credentials before continuing.${NC}"
    read -p "Press ENTER after editing .env to continue..."
fi

# ── App key ──────────────────────────────────────────────────
echo -e "\n${YELLOW}Generating application key...${NC}"
php artisan key:generate
echo -e "${GREEN}✓ App key generated${NC}"

# ── Storage permissions ───────────────────────────────────────
echo -e "\n${YELLOW}Setting permissions...${NC}"
chmod -R 755 storage bootstrap/cache
find storage -type f -exec chmod 644 {} \;
echo -e "${GREEN}✓ Permissions set${NC}"

# ── Database migration ────────────────────────────────────────
echo -e "\n${YELLOW}Running migrations...${NC}"
php artisan migrate --force
echo -e "${GREEN}✓ Migrations complete${NC}"

# ── Seed devices ─────────────────────────────────────────────
echo -e "\n${YELLOW}Seeding LIS devices...${NC}"
php artisan db:seed --force
echo -e "${GREEN}✓ Devices seeded (KM8.1 NB + KM6.1 NB)${NC}"

# ── Cache ─────────────────────────────────────────────────────
echo -e "\n${YELLOW}Caching configuration...${NC}"
php artisan config:cache
php artisan route:cache
php artisan view:cache
echo -e "${GREEN}✓ Caches built${NC}"

# ── Storage link ──────────────────────────────────────────────
echo -e "\n${YELLOW}Creating storage symlink...${NC}"
php artisan storage:link 2>/dev/null || echo "(symlink may already exist)"
echo -e "${GREEN}✓ Storage linked${NC}"

echo -e "\n${GREEN}============================================${NC}"
echo -e "${GREEN} LIS Portal deployed successfully!${NC}"
echo -e "${GREEN}============================================${NC}"
echo -e "Add this cron job in cPanel → Cron Jobs:"
echo -e "  ${YELLOW}* * * * * $(which php) $(pwd)/artisan schedule:run >> /dev/null 2>&1${NC}"
echo ""
