#!/bin/bash
set -euo pipefail

# =============================================================================
# KHWWC Targeted Deploy - STK Push Fixes (Backend + Frontend)
# =============================================================================
# Run from: cd /home/kirinyag/public_html/app1.kirinyagahealthcareworkerswelfare.co.ke && bash deploy-stk-fixes.sh
# =============================================================================

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

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

info() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }

echo "============================================"
echo " KHWWC STK Push Fix Deployment"
echo "============================================"
echo "Directory: $SCRIPT_DIR"
echo ""

# Verify khwwc directory exists
if [ ! -d "$KHWWC_DIR" ]; then
    error "khwwc directory not found at: $KHWWC_DIR"
    error "Run this from the public_html root where khwwc/ lives"
    exit 1
fi

# =============================================================================
# STEP 1: Backend file deployment
# =============================================================================
info "Deploying backend fixes..."

# Source files (local)
SRC_FRONTEND_PAYPENALTY="/home/laban/Documents/kirinyaga App/frontend/src/pages/member/PayPenalty.tsx"
SRC_RECONCILE="/home/laban/Documents/kirinyaga App/khwwc/app/Console/Commands/CoopReconcileTransfers.php"
SRC_STK_SERVICE="/home/laban/Documents/kirinyaga App/khwwc/app/Services/CoopBank/CoopBankTransactionService.php"

# Destination files (production)
DEST_FRONTEND_PAYPENALTY="${KHWWC_DIR/public/build/assets/*.js"
# Actually for Laravel, the built frontend goes into public/build or similar
# But PayPenalty.tsx is a source file - we need to upload the built dist
DEST_RECONCILE="${KHWWC_DIR}/app/Console/Commands/CoopReconcileTransfers.php"
DEST_STK_SERVICE="${KHWWC_DIR}/app/Services/CoopBank/CoopBankTransactionService.php"

# Check source files exist
if [ ! -f "$SRC_FRONTEND_PAYPENALTY" ]; then
    error "Source file not found: $SRC_FRONTEND_PAYPENALTY"
    exit 1
fi
if [ ! -f "$SRC_RECONCILE" ]; then
    error "Source file not found: $SRC_RECONCILE"
    exit 1
fi
if [ ! -f "$SRC_STK_SERVICE" ]; then
    error "Source file not found: $SRC_STK_SERVICE"
    exit 1
fi

# Backup existing files
timestamp=$(date +%Y%m%d_%H%M%S)

if [ -f "$DEST_RECONCILE" ]; then
    cp "$DEST_RECONCILE" "${DEST_RECONCILE}.backup.${timestamp}"
    success "Backed up CoopReconcileTransfers.php"
fi

if [ -f "$DEST_STK_SERVICE" ]; then
    cp "$DEST_STK_SERVICE" "${DEST_STK_SERVICE}.backup.${timestamp}"
    success "Backed up CoopBankTransactionService.php"
fi

# Deploy modified PHP files
cp "$SRC_RECONCILE" "$DEST_RECONCILE"
success "Deployed CoopReconcileTransfers.php"

cp "$SRC_STK_SERVICE" "$DEST_STK_SERVICE"
success "Deployed CoopBankTransactionService.php"

# =============================================================================
# STEP 2: Frontend deployment
# =============================================================================
info "Deploying frontend build..."

FRONTEND_DIST="/home/laban/Documents/kirinyaga App/frontend/dist"
PUBLIC_DIR="${KHWWC_DIR}/public"

if [ ! -d "$FRONTEND_DIST" ]; then
    error "Frontend dist not found at: $FRONTEND_DIST"
    error "Run 'npm run build' in the frontend directory first"
    exit 1
fi

# Backup existing build
if [ -d "${PUBLIC_DIR}/build" ]; then
    mv "${PUBLIC_DIR}/build" "${PUBLIC_DIR}/build.backup.${timestamp}"
    success "Backed up existing build/"
fi

# Deploy new build
cp -r "$FRONTEND_DIST" "${PUBLIC_DIR}/build"
success "Deployed frontend build to public/build/"

# =============================================================================
# STEP 3: Laravel optimization
# =============================================================================
info "Running Laravel optimization..."

cd "$KHWWC_DIR"

php artisan config:clear --no-interaction || true
php artisan route:clear --no-interaction || true
php artisan view:clear --no-interaction || true
php artisan cache:clear --no-interaction || true

php artisan config:cache --no-interaction
php artisan route:cache --no-interaction
success "Laravel optimization complete"

echo ""
echo "============================================"
echo " Deployment Complete"
echo "============================================"
echo ""
echo "Deployed files:"
echo "  Backend:"
echo "    - $DEST_RECONCILE"
echo "    - $DEST_STK_SERVICE"
echo "  Frontend:"
echo "    - ${PUBLIC_DIR}/build/ (from frontend/dist/)"
echo ""
echo "Verify:"
echo "  https://app1.kirinyagahealthcareworkerswelfare.co.ke/api/health"
echo ""
