# Makefile for Switchboard JavaScript SDK
# This integrates with the main Kazoo build system

.PHONY: all deps test test-coverage build clean distclean

# Node and NPM detection
NODE := $(shell which node 2>/dev/null)
NPM := $(shell which npm 2>/dev/null)
YARN := $(shell which yarn 2>/dev/null)

# Use yarn if available, otherwise npm
ifdef YARN
	PKG_MANAGER := yarn
	INSTALL_CMD := yarn install --frozen-lockfile
	TEST_CMD := yarn test
	BUILD_CMD := yarn build
else
	PKG_MANAGER := npm
	INSTALL_CMD := npm ci
	TEST_CMD := npm test
	BUILD_CMD := npm run build
endif

# Check for Node.js
check-node:
ifndef NODE
	$(error Node.js is not installed. Please install Node.js 14+ to build the JavaScript SDK)
endif
	@echo "Using Node.js: $(NODE)"
	@echo "Node version: $(shell node --version)"
	@echo "Package manager: $(PKG_MANAGER)"

# Install dependencies
deps: check-node package.json
	@echo "Installing JavaScript SDK dependencies..."
	@$(INSTALL_CMD)
	@touch node_modules/.installed

node_modules/.installed: package.json
	@$(MAKE) deps

# Run tests
test: node_modules/.installed
	@echo "Running JavaScript SDK tests..."
	@NODE_ENV=test $(TEST_CMD) --silent

# Run tests with coverage
test-coverage: node_modules/.installed
	@echo "Running JavaScript SDK tests with coverage..."
	@NODE_ENV=test $(PKG_MANAGER) run test:coverage

# Run tests in CI mode (no watch, with coverage)
test-ci: node_modules/.installed
	@echo "Running JavaScript SDK tests in CI mode..."
	@NODE_ENV=test CI=true $(PKG_MANAGER) test -- --coverage --watchAll=false

# Build the SDK
build: node_modules/.installed
	@echo "Building JavaScript SDK..."
	@$(BUILD_CMD)
	@echo "JavaScript SDK built successfully"

# Build for production
build-prod: node_modules/.installed
	@echo "Building JavaScript SDK for production..."
	@NODE_ENV=production $(BUILD_CMD)

# Lint the code
lint: node_modules/.installed
	@echo "Linting JavaScript SDK..."
	@$(PKG_MANAGER) run lint

# Format the code
format: node_modules/.installed
	@echo "Formatting JavaScript SDK..."
	@$(PKG_MANAGER) run format

# Clean build artifacts
clean:
	@echo "Cleaning JavaScript SDK build artifacts..."
	@rm -rf dist coverage .nyc_output
	@rm -f junit.xml

# Deep clean including dependencies
distclean: clean
	@echo "Removing JavaScript SDK dependencies..."
	@rm -rf node_modules package-lock.json yarn.lock

# Development mode with watch
dev: node_modules/.installed
	@echo "Starting JavaScript SDK in development mode..."
	@$(PKG_MANAGER) run dev

# Verify the SDK is ready for release
verify: lint test build
	@echo "JavaScript SDK verification complete"

# Create distribution archive
dist: build
	@echo "Creating distribution archive..."
	@rm -rf switchboard-sdk-dist
	@mkdir -p switchboard-sdk-dist
	@cp -r dist switchboard-sdk-dist/
	@cp -r src switchboard-sdk-dist/
	@cp package.json switchboard-sdk-dist/
	@cp README.md switchboard-sdk-dist/
	@cp rollup.config.js switchboard-sdk-dist/
	@cp jest.config.js switchboard-sdk-dist/
	@cp .babelrc switchboard-sdk-dist/
	@echo '{"registry":"https://registry.npmjs.org/"}' > switchboard-sdk-dist/.npmrc
	@tar -czf switchboard-sdk-$(shell node -p "require('./package.json').version").tar.gz switchboard-sdk-dist
	@rm -rf switchboard-sdk-dist
	@echo "Distribution archive created: switchboard-sdk-$(shell node -p "require('./package.json').version").tar.gz"

# Default target
all: test build

# Help target
help:
	@echo "Switchboard JavaScript SDK Make Targets:"
	@echo "  make deps          - Install dependencies"
	@echo "  make test          - Run tests"
	@echo "  make test-coverage - Run tests with coverage report"
	@echo "  make test-ci       - Run tests in CI mode"
	@echo "  make build         - Build the SDK"
	@echo "  make build-prod    - Build for production"
	@echo "  make lint          - Lint the code"
	@echo "  make format        - Format the code"
	@echo "  make clean         - Clean build artifacts"
	@echo "  make distclean     - Clean everything including dependencies"
	@echo "  make dev           - Start development mode with watch"
	@echo "  make verify        - Run all checks (lint, test, build)"
	@echo "  make dist          - Create distribution archive"
	@echo "  make help          - Show this help message"