#!/bin/bash

# Test script for WSL2 environment
# This script copies the project to WSL2, installs dependencies, and runs tests

set -e

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

echo -e "${BLUE}=== Admin Service WSL2 Testing ===${NC}"

# Configuration
PROJECT_NAME="admin-service"
WSL_TEMP_DIR="/tmp/${PROJECT_NAME}-test-$(date +%s)"

echo -e "${YELLOW}1. Creating temporary directory in WSL: ${WSL_TEMP_DIR}${NC}"
mkdir -p "${WSL_TEMP_DIR}"

echo -e "${YELLOW}2. Copying project files to WSL (excluding node_modules)...${NC}"

# Copy only necessary files, exclude node_modules and build artifacts
rsync -av --progress \
  --exclude='node_modules' \
  --exclude='dist' \
  --exclude='coverage' \
  --exclude='.git' \
  --exclude='*.log' \
  ./ "${WSL_TEMP_DIR}/"

echo -e "${GREEN}✓ Files copied${NC}"

cd "${WSL_TEMP_DIR}"

echo -e "${YELLOW}3. Installing dependencies in WSL...${NC}"
npm ci

echo -e "${YELLOW}4. Generating Prisma client...${NC}"
DATABASE_URL="postgresql://postgres:password@localhost:5432/admin_service_test" npm run prisma:generate

echo -e "${YELLOW}5. Running unit tests...${NC}"
npm run test:unit

echo -e "${YELLOW}6. Running integration tests...${NC}"
echo -e "${YELLOW}   Note: Make sure PostgreSQL is running and test database exists${NC}"
npm run test:integration

echo -e "${YELLOW}7. Running E2E tests...${NC}"
npm run test:e2e

echo -e "${YELLOW}8. Generating coverage report...${NC}"
npm run test:cov

echo -e "${GREEN}=== All tests passed! ===${NC}"
echo -e "${BLUE}Coverage report available at: ${WSL_TEMP_DIR}/coverage${NC}"

# Optionally clean up
read -p "Clean up temporary directory? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Cleaning up...${NC}"
    cd /tmp
    rm -rf "${WSL_TEMP_DIR}"
    echo -e "${GREEN}✓ Cleanup complete${NC}"
fi
