#!/bin/bash
# Pre-commit hook to validate package-lock.json is in sync with package.json

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

# Check if package.json is being committed in webui directory
if git diff --cached --name-only | grep -q "^webui/package\.json$"; then
    echo -e "${YELLOW}Detected changes to webui/package.json...${NC}"
    
    # Check if package-lock.json is also staged
    if ! git diff --cached --name-only | grep -q "^webui/package-lock\.json$"; then
        echo -e "${RED}ERROR: webui/package.json is staged but webui/package-lock.json is not!${NC}"
        echo -e "${YELLOW}Please run: cd webui && npm install${NC}"
        echo -e "${YELLOW}Then stage: git add webui/package-lock.json${NC}"
        exit 1
    fi
    
    # Validate that package-lock.json is in sync
    cd webui
    if ! npm ls &>/dev/null; then
        echo -e "${RED}ERROR: package-lock.json appears to be out of sync with package.json${NC}"
        echo -e "${YELLOW}Please run: npm install${NC}"
        echo -e "${YELLOW}Then stage: git add package-lock.json${NC}"
        cd ..
        exit 1
    fi
    cd ..
    
    echo -e "${GREEN}✓ package.json and package-lock.json are in sync${NC}"
fi

exit 0

