#!/bin/bash
# Run all the external tests

# -- Setup --

# Fail if any command fails
set -euo pipefail

# -- Console Output --

# Define colors so we can print colorful text
color_red="\033[0;31m"
color_green="\033[0;32m"
color_yellow="\033[0;33m"
color_cyan="\033[0;36m"
color_dimmed="\033[1;30m"
color_normal="\033[0m"

# Define output helper functions
# Print functions with different colors
function print_success {
	echo -e "${color_green}=> $1${color_normal}\n"
}
function print_warning {
	echo -e "${color_yellow}=> $1${color_normal}\n"
}
function print_error {
	echo -e "${color_red}=> $1${color_normal}\n"
}
function print_info {
	echo -e "${color_cyan}=> $1${color_normal}\n"
}

# -- Utility Functions --

# Recreates the docker containers needed for the external store tests. 
function start_docker {
	print_info "stopping docker containers"
	# Stop and delete any existing containers
	docker stop rate-limit-memcached rate-limit-mongo rate-limit-redis || true
	docker rm rate-limit-memcached rate-limit-mongo rate-limit-redis || true

	# Database directory for the mongo container
	data_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'rate-limit-mongo-database')

	print_info "restarting docker containers"
	# Start all the containers
	docker run --name rate-limit-memcached -p 11211:11211 -d memcached
	docker run --name rate-limit-mongo -p 27017:27017 -v $data_dir:/data/db -d mongo
	docker run --name rate-limit-redis -p 6379:6379 -d redis

	# Wait for a few seconds
	sleep 10

	print_success "successfully started docker containers"
}

# -- Main --

# Tests all the variations (JS/TS and CJS/ESM) with the package.
function run_import_tests {
	print_info "running import tests"

	# cd into the imports folder
	cd imports/
	# For each type of import (named and default), run the tests
	for type in "named" "default"; do
		cd "$type-import"
		
		for dir in `ls -d */`; do
			print_info "running $dir variation for $type imports (express 4)"
			# cd into the example project
			cd $dir

			# Get a fresh install of all node modules
			pnpm install
			# Run the linter and the tests
			pnpm lint
			pnpm test
			echo
			print_success "sucessfully ran $dir variation for $type imports (express 4)"

			print_info "running $dir variation for $type imports (express 5)"
			# Install Express v5
			pnpm install express@5.0.0-beta.1
			# Run the tests with this version
			pnpm lint
			pnpm test
			# Restore the version
			git restore package.json pnpm-lock.yaml
			echo
			print_success "sucessfully ran $dir variation for $type imports (express 5)"

			# Go back into the next variation
			cd ../
		done
		
		# Go back
		cd ../
	done

	# Go back
	cd ../

	print_success "successfully ran all import tests"
}

# Tests all external stores with the package.
function run_store_tests {
	print_info "running store tests"

	# cd into the example project
	cd stores/

	# Start the containers
	start_docker

	# Get a fresh install of all the node modules
	pnpm install

	print_info "running store tests (express 4)"
	# Run the tests with Express v4
	pnpm test

	# Restart the containers
	start_docker

	print_info "running store tests (express 5)"
	# Install Express v5
	pnpm install express@5.0.0-beta.1
	# Run the tests with this version
	pnpm test
	# Restore the version
	git restore package.json pnpm-lock.yaml

	# Go back
	cd ../

	print_info "stopping docker containers"
	# Stop and delete any existing containers
	docker stop rate-limit-memcached rate-limit-mongo rate-limit-redis || true
	docker rm rate-limit-memcached rate-limit-mongo rate-limit-redis || true

	print_success "successfully ran all store tests"
}

# Upgrades all dependencies
function upgrade_dependencies {
	print_info "upgrading project dependencies"

	# cd into the imports folder
	cd imports/
	# For each type of import (named and default), run the tests
	for type in "named" "default"; do
		cd "$type-import"
		
		for dir in `ls -d */`; do
			print_info "upgrading dependencies of $type-import/$dir"
			# cd into the example project
			cd $dir

			# Get a fresh install of all node modules
			rm -rf node_modules
			pnpm upgrade -L
			echo
			print_success "sucessfully upgraded dependencies of $type-import/$dir"

			# Go back into the next variation
			cd ../
		done
		
		# Go back
		cd ../
	done

	# Go back
	cd ../

	print_success "successfully upgraded dependencies"
}

# Run the tests!
run_import_tests
run_store_tests

# Upgrade dependencies!
# upgrade_dependencies
