#!/bin/bash

# VoiceCast Admin Panel Test Runner
# This script provides easy commands to run different types of tests

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

# Function to print colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Function to check if dependencies are installed
check_dependencies() {
    print_status "Checking dependencies..."
    
    if ! command -v node &> /dev/null; then
        print_error "Node.js is not installed. Please install Node.js first."
        exit 1
    fi
    
    if ! command -v npm &> /dev/null; then
        print_error "npm is not installed. Please install npm first."
        exit 1
    fi
    
    if [ ! -f "package.json" ]; then
        print_error "package.json not found. Please run this script from the project root."
        exit 1
    fi
    
    print_success "Dependencies check passed"
}

# Function to install test dependencies
install_test_deps() {
    print_status "Installing test dependencies..."
    
    if npm install --save-dev jest ts-jest @types/jest supertest @types/supertest mongodb-memory-server; then
        print_success "Test dependencies installed successfully"
    else
        print_error "Failed to install test dependencies"
        exit 1
    fi
}

# Function to run all tests
run_all_tests() {
    print_status "Running all tests..."
    
    if npm test; then
        print_success "All tests passed!"
    else
        print_error "Some tests failed"
        exit 1
    fi
}

# Function to run tests with coverage
run_coverage_tests() {
    print_status "Running tests with coverage..."
    
    if npm run test:coverage; then
        print_success "Coverage tests completed!"
        print_status "Coverage report generated in coverage/ directory"
    else
        print_error "Coverage tests failed"
        exit 1
    fi
}

# Function to run admin panel tests only
run_admin_tests() {
    print_status "Running admin panel tests..."
    
    if npm run test:admin; then
        print_success "Admin panel tests passed!"
    else
        print_error "Admin panel tests failed"
        exit 1
    fi
}

# Function to run unit tests only
run_unit_tests() {
    print_status "Running unit tests..."
    
    if npm run test:unit; then
        print_success "Unit tests passed!"
    else
        print_error "Unit tests failed"
        exit 1
    fi
}

# Function to run integration tests only
run_integration_tests() {
    print_status "Running integration tests..."
    
    if npm run test:integration; then
        print_success "Integration tests passed!"
    else
        print_error "Integration tests failed"
        exit 1
    fi
}

# Function to run tests in watch mode
run_watch_tests() {
    print_status "Starting tests in watch mode..."
    print_status "Press Ctrl+C to stop watching"
    
    npm run test:watch
}

# Function to run specific test file
run_specific_test() {
    local test_file="$1"
    
    if [ -z "$test_file" ]; then
        print_error "Please specify a test file path"
        echo "Usage: $0 specific <test-file-path>"
        exit 1
    fi
    
    if [ ! -f "$test_file" ]; then
        print_error "Test file not found: $test_file"
        exit 1
    fi
    
    print_status "Running specific test: $test_file"
    
    if npm test -- "$test_file"; then
        print_success "Test passed!"
    else
        print_error "Test failed"
        exit 1
    fi
}

# Function to run tests with pattern matching
run_pattern_tests() {
    local pattern="$1"
    
    if [ -z "$pattern" ]; then
        print_error "Please specify a test pattern"
        echo "Usage: $0 pattern <test-pattern>"
        exit 1
    fi
    
    print_status "Running tests matching pattern: $pattern"
    
    if npm test -- --testNamePattern="$pattern"; then
        print_success "Pattern tests passed!"
    else
        print_error "Pattern tests failed"
        exit 1
    fi
}

# Function to clean test artifacts
clean_test_artifacts() {
    print_status "Cleaning test artifacts..."
    
    # Remove coverage directory
    if [ -d "coverage" ]; then
        rm -rf coverage
        print_success "Removed coverage directory"
    fi
    
    # Remove test cache
    if [ -d "node_modules/.cache" ]; then
        rm -rf node_modules/.cache
        print_success "Removed test cache"
    fi
    
    print_success "Test artifacts cleaned"
}

# Function to show test status
show_test_status() {
    print_status "Test Status:"
    echo "  - Total test files: $(find src/tests -name "*.test.ts" | wc -l)"
    echo "  - Admin tests: $(find src/tests -name "*admin*.test.ts" | wc -l)"
    echo "  - Controller tests: $(find src/tests/controllers -name "*.test.ts" | wc -l)"
    echo "  - Middleware tests: $(find src/tests/middleware -name "*.test.ts" | wc -l)"
    echo "  - Utility tests: $(find src/tests/utils -name "*.test.ts" | wc -l)"
    echo "  - Class tests: $(find src/tests/classes -name "*.test.ts" | wc -l)"
}

# Function to show help
show_help() {
    echo "VoiceCast Admin Panel Test Runner"
    echo ""
    echo "Usage: $0 [COMMAND] [OPTIONS]"
    echo ""
    echo "Commands:"
    echo "  all              Run all tests"
    echo "  coverage         Run tests with coverage report"
    echo "  admin            Run admin panel tests only"
    echo "  unit             Run unit tests only"
    echo "  integration      Run integration tests only"
    echo "  watch            Run tests in watch mode"
    echo "  specific <file>  Run specific test file"
    echo "  pattern <regex>  Run tests matching pattern"
    echo "  clean            Clean test artifacts"
    echo "  status           Show test status"
    echo "  install          Install test dependencies"
    echo "  help             Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 all                    # Run all tests"
    echo "  $0 coverage               # Run with coverage"
    echo "  $0 specific src/tests/admin/admin.test.ts"
    echo "  $0 pattern 'User Management'"
    echo "  $0 watch                  # Watch mode"
}

# Main script logic
main() {
    local command="$1"
    local option="$2"
    
    # Check dependencies first
    check_dependencies
    
    case "$command" in
        "all")
            run_all_tests
            ;;
        "coverage")
            run_coverage_tests
            ;;
        "admin")
            run_admin_tests
            ;;
        "unit")
            run_unit_tests
            ;;
        "integration")
            run_integration_tests
            ;;
        "watch")
            run_watch_tests
            ;;
        "specific")
            run_specific_test "$option"
            ;;
        "pattern")
            run_pattern_tests "$option"
            ;;
        "clean")
            clean_test_artifacts
            ;;
        "status")
            show_test_status
            ;;
        "install")
            install_test_deps
            ;;
        "help"|"--help"|"-h"|"")
            show_help
            ;;
        *)
            print_error "Unknown command: $command"
            echo ""
            show_help
            exit 1
            ;;
    esac
}

# Run main function with all arguments
main "$@"
