#!/bin/bash

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}🧪 Testing Node.js dependencies for Debian package...${NC}"

# Check if Node.js is available
if command -v node &> /dev/null; then
    NODE_VERSION=$(node --version)
    echo -e "${GREEN}✅ Node.js found: ${NODE_VERSION}${NC}"
else
    echo -e "${RED}❌ Node.js not found${NC}"
    exit 1
fi

# Check if npm is available
if command -v npm &> /dev/null; then
    NPM_VERSION=$(npm --version)
    echo -e "${GREEN}✅ npm found: ${NPM_VERSION}${NC}"
else
    echo -e "${RED}❌ npm not found${NC}"
    exit 1
fi

# Check Node.js version requirement
NODE_MAJOR=$(node --version | sed 's/v\([0-9]*\).*/\1/')
if [ "$NODE_MAJOR" -ge 18 ]; then
    echo -e "${GREEN}✅ Node.js version ${NODE_MAJOR} meets requirement (>= 18)${NC}"
else
    echo -e "${RED}❌ Node.js version ${NODE_MAJOR} does not meet requirement (>= 18)${NC}"
    exit 1
fi

# Check if we can read package.json
if [ -f "package.json" ]; then
    PACKAGE_VERSION=$(node -p "require('./package.json').version")
    echo -e "${GREEN}✅ package.json readable, version: ${PACKAGE_VERSION}${NC}"
else
    echo -e "${RED}❌ package.json not found${NC}"
    exit 1
fi

# Test dpkg-buildpackage dependencies
echo -e "${YELLOW}🔍 Checking dpkg-buildpackage dependencies...${NC}"
if command -v dpkg-checkbuilddeps &> /dev/null; then
    echo -e "${GREEN}✅ dpkg-checkbuilddeps available${NC}"
    
    # Check if we can run dpkg-checkbuilddeps (this might fail, but that's okay for testing)
    if dpkg-checkbuilddeps debian/control 2>&1 | grep -q "Unmet build dependencies"; then
        echo -e "${YELLOW}⚠️ Some build dependencies unmet, but this is expected in CI environment${NC}"
    else
        echo -e "${GREEN}✅ All build dependencies satisfied${NC}"
    fi
else
    echo -e "${YELLOW}⚠️ dpkg-checkbuilddeps not available (not on Debian/Ubuntu)${NC}"
fi

echo -e "${GREEN}✅ Node.js dependency test completed!${NC}" 