#!/bin/bash

# PineHealth Backend Installation Verification Script
# This script checks that all components are properly installed

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║        PineHealth Backend Installation Verification            ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

# Color codes
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

ERRORS=0

# Check Node.js
echo "Checking Node.js..."
if command -v node &> /dev/null; then
    NODE_VERSION=$(node -v)
    echo -e "${GREEN}✓${NC} Node.js is installed: $NODE_VERSION"
else
    echo -e "${RED}✗${NC} Node.js is not installed"
    ERRORS=$((ERRORS + 1))
fi

# Check npm
echo "Checking npm..."
if command -v npm &> /dev/null; then
    NPM_VERSION=$(npm -v)
    echo -e "${GREEN}✓${NC} npm is installed: $NPM_VERSION"
else
    echo -e "${RED}✗${NC} npm is not installed"
    ERRORS=$((ERRORS + 1))
fi

# Check if in correct directory
if [ ! -f "package.json" ]; then
    echo -e "${RED}✗${NC} Not in backend directory. Please run from backend/ folder"
    exit 1
fi

# Check node_modules
echo "Checking dependencies..."
if [ -d "node_modules" ]; then
    PACKAGE_COUNT=$(ls node_modules | wc -l)
    echo -e "${GREEN}✓${NC} Dependencies installed: $PACKAGE_COUNT packages"
else
    echo -e "${YELLOW}⚠${NC} Dependencies not installed. Run: npm install"
    ERRORS=$((ERRORS + 1))
fi

# Check .env file
echo "Checking configuration..."
if [ -f ".env" ]; then
    echo -e "${GREEN}✓${NC} .env file exists"
else
    echo -e "${YELLOW}⚠${NC} .env file not found. Copy .env.example to .env and configure it"
fi

# Check directory structure
echo "Checking project structure..."
REQUIRED_DIRS=(
    "src"
    "src/config"
    "src/controllers"
    "src/middleware"
    "src/models"
    "src/routes"
    "src/utils"
    "uploads"
)

for dir in "${REQUIRED_DIRS[@]}"; do
    if [ -d "$dir" ]; then
        echo -e "${GREEN}✓${NC} Directory exists: $dir"
    else
        echo -e "${RED}✗${NC} Missing directory: $dir"
        ERRORS=$((ERRORS + 1))
    fi
done

# Check key files
echo "Checking key files..."
KEY_FILES=(
    "src/server.js"
    "src/config/database.js"
    "src/models/index.js"
    ".env.example"
    "README.md"
)

for file in "${KEY_FILES[@]}"; do
    if [ -f "$file" ]; then
        echo -e "${GREEN}✓${NC} File exists: $file"
    else
        echo -e "${RED}✗${NC} Missing file: $file"
        ERRORS=$((ERRORS + 1))
    fi
done

# Count files
echo ""
echo "Project Statistics:"
JS_FILES=$(find src -name "*.js" | wc -l)
MODELS=$(ls src/models/*.js 2>/dev/null | grep -v index.js | wc -l)
CONTROLLERS=$(ls src/controllers/*.js 2>/dev/null | wc -l)
ROUTES=$(ls src/routes/*.js 2>/dev/null | wc -l)
LINES=$(find src -name "*.js" -exec cat {} \; | wc -l)

echo "  • JavaScript files: $JS_FILES"
echo "  • Models: $MODELS"
echo "  • Controllers: $CONTROLLERS"
echo "  • Routes: $ROUTES"
echo "  • Total lines of code: $LINES"

# JavaScript syntax check
echo ""
echo "Checking JavaScript syntax..."
SYNTAX_ERRORS=0
for file in $(find src -name "*.js"); do
    if ! node --check "$file" 2>/dev/null; then
        echo -e "${RED}✗${NC} Syntax error in: $file"
        SYNTAX_ERRORS=$((SYNTAX_ERRORS + 1))
    fi
done

if [ $SYNTAX_ERRORS -eq 0 ]; then
    echo -e "${GREEN}✓${NC} All JavaScript files are syntactically correct"
else
    echo -e "${RED}✗${NC} Found $SYNTAX_ERRORS files with syntax errors"
    ERRORS=$((ERRORS + SYNTAX_ERRORS))
fi

# Summary
echo ""
echo "════════════════════════════════════════════════════════════════"
if [ $ERRORS -eq 0 ]; then
    echo -e "${GREEN}✓ Installation verified successfully!${NC}"
    echo ""
    echo "Next steps:"
    echo "  1. Configure .env file with your database credentials"
    echo "  2. Ensure MySQL is running and database exists"
    echo "  3. Run 'npm run dev' to start the development server"
    echo "  4. Test the API at http://localhost:3000/health"
    exit 0
else
    echo -e "${RED}✗ Installation verification failed with $ERRORS errors${NC}"
    echo ""
    echo "Please fix the errors above and run this script again."
    exit 1
fi
