#!/bin/bash

set -euo pipefail

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT="dehydrate"
DEHYDRATE="${SCRIPT_DIR}/output/${PROJECT}"
TEST_DIR="${SCRIPT_DIR}/test_temp"

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

# Test counters
TESTS_PASSED=0
TESTS_FAILED=0

# Function to print test results
print_test_result() {
    local test_name="$1"
    local result="$2"
    if [ "$result" -eq 0 ]; then
        echo -e "${GREEN}✓${NC} $test_name"
        TESTS_PASSED=$((TESTS_PASSED + 1))
    else
        echo -e "${RED}✗${NC} $test_name"
        TESTS_FAILED=$((TESTS_FAILED + 1))
    fi
}

# Function to cleanup test artifacts
cleanup() {
    echo -e "\n${YELLOW}Cleaning up test artifacts...${NC}"
    rm -rf "$TEST_DIR"
    echo -e "\nDone.\n"
}

# Set up trap to ensure cleanup runs
trap cleanup EXIT

# Create test directory
mkdir -p "$TEST_DIR"

echo -e "${YELLOW}Running dehydrate tests...${NC}\n"

# Check if dehydrate binary exists
if [ ! -f "$DEHYDRATE" ]; then
    echo -e "${RED}Error: dehydrate binary not found at $DEHYDRATE${NC}"
    echo "Please run ./build.sh first to build dehydrate"
    exit 1
fi

if [ ! -x "$DEHYDRATE" ]; then
    echo -e "${RED}Error: dehydrate binary is not executable${NC}"
    exit 1
fi

echo "Using dehydrate binary: $DEHYDRATE"

# Test 1: Version command (dehydrate -v shows version only)
echo "Test 1: Version command"
VERSION_OUTPUT=$("$DEHYDRATE" -v 2>&1 || true)
# Version output should be just the version number
VERSION=$(echo "$VERSION_OUTPUT" | head -n 1)
if [[ "$VERSION" =~ ^[0-9]{4}\.[0-9]{4}\.[0-9]{4}$ ]]; then
    print_test_result "Version format (YYYY.MMDD.HHMM)" 0
else
    print_test_result "Version format (YYYY.MMDD.HHMM)" 1
fi

# Test 2: Help command (shows help when no args provided)
printf "\nTest 2: Help command\n"
HELP_OUTPUT=$("$DEHYDRATE" 2>&1 || true)
if [[ "$HELP_OUTPUT" =~ "Usage: dehydrate" ]] && [[ "$HELP_OUTPUT" =~ "Converts existing files" ]]; then
    print_test_result "Help command output" 0
else
    print_test_result "Help command output" 1
fi

# Test 3: Basic dehydration test
echo -e "\nTest 3: Basic dehydration test"
# Create a test source directory
TEST_SRC_DIR="${TEST_DIR}/test_src"
mkdir -p "$TEST_SRC_DIR"
echo "int main() { return 0; }" > "$TEST_SRC_DIR/main.cpp"
echo "#include <iostream>" > "$TEST_SRC_DIR/header.hpp"

# Run dehydrate on the test source
"$DEHYDRATE" -s "$TEST_SRC_DIR" "$TEST_DIR"
# Dehydrate creates files with pattern _<source_dir_name>.{cpp,hpp}
if [ -f "$TEST_DIR/_test_src.hpp" ] && [ -f "$TEST_DIR/_test_src.cpp" ]; then
    print_test_result "Basic dehydration creates output files" 0
else
    print_test_result "Basic dehydration creates output files" 1
fi

# Test 4: Test the generated files have valid syntax
echo -e "\nTest 4: Test generated files syntax"
if [ -f "$TEST_DIR/_test_src.hpp" ] && [ -f "$TEST_DIR/_test_src.cpp" ]; then
    # Check that the header file has the expected namespace declaration
    if grep -q "namespace recreate_test_src" "$TEST_DIR/_test_src.hpp"; then
        # Check that the cpp file includes the header
        if grep -q "#include \"_test_src.hpp\"" "$TEST_DIR/_test_src.cpp"; then
            # Check that the function is declared
            if grep -q "recreate_tree" "$TEST_DIR/_test_src.hpp"; then
                print_test_result "Generated files have correct structure" 0
            else
                print_test_result "Generated files have correct structure" 1
            fi
        else
            print_test_result "Generated files have correct structure" 1
        fi
    else
        print_test_result "Generated files have correct structure" 1
    fi
else
    print_test_result "Generated files have correct structure" 1
fi

cleanup

# Print summary for basic tests
echo -e "\n${YELLOW}Basic Test Summary:${NC}"
echo -e "Tests passed: ${GREEN}${TESTS_PASSED}${NC}"
echo -e "Tests failed: ${RED}${TESTS_FAILED}${NC}"

# Run comprehensive tests if basic tests passed
if [ "$TESTS_FAILED" -eq 0 ]; then
    echo -e "\n${YELLOW}Running comprehensive tests...${NC}"
    if [ -f "$SCRIPT_DIR/test/test.sh" ]; then
        cd "$SCRIPT_DIR/test"
        ./test.sh
        COMPREHENSIVE_RESULT=$?
        cd "$SCRIPT_DIR"
        
        if [ "$COMPREHENSIVE_RESULT" -eq 0 ]; then
            echo -e "\n${GREEN}All tests passed!${NC}"
            exit 0
        else
            echo -e "\n${RED}Comprehensive tests failed!${NC}"
            exit 1
        fi
    else
        echo -e "${YELLOW}Warning: Comprehensive test suite not found${NC}"
        echo -e "\n${GREEN}Basic tests passed!${NC}"
        exit 0
    fi
else
    echo -e "\n${RED}Basic tests failed! Skipping comprehensive tests.${NC}"
    exit 1
fi