#!/bin/bash

set -euo pipefail

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT="bb64"
BB64="$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"
}

# Set up trap to ensure cleanup runs
trap cleanup EXIT

# Create test directory
mkdir -p "$TEST_DIR"

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

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

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

echo "Using bb64 binary: $BB64"

# Test 1: Version command with -v flag
echo "Test 1: Version command (-v flag)"
VERSION_OUTPUT=$("$BB64" -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 with -v flag (YYYY.MMDD.HHMM)" 0
else
    print_test_result "Version format with -v flag (YYYY.MMDD.HHMM)" 1
    echo "  Expected: YYYY.MMDD.HHMM format, got: '$VERSION'"
fi

# Test 2: Version command with 'version' argument
printf "\nTest 2: Version command (version argument)\n"
VERSION_OUTPUT2=$("$BB64" version 2>&1 || true)
# Version output should be just the version number
VERSION2=$(echo "$VERSION_OUTPUT2" | head -n 1)
if [[ "$VERSION2" =~ ^[0-9]{4}\.[0-9]{4}\.[0-9]{4}$ ]]; then
    print_test_result "Version format with 'version' argument (YYYY.MMDD.HHMM)" 0
else
    print_test_result "Version format with 'version' argument (YYYY.MMDD.HHMM)" 1
    echo "  Expected: YYYY.MMDD.HHMM format, got: '$VERSION2'"
fi

# Test 3: Both version commands should return the same version
printf "\nTest 3: Version consistency\n"
if [ "$VERSION" = "$VERSION2" ]; then
    print_test_result "Both -v and version return same version" 0
else
    print_test_result "Both -v and version return same version" 1
    echo "  -v returned: '$VERSION'"
    echo "  version returned: '$VERSION2'"
fi

# Test 4: Basic encoding test
echo -e "\nTest 4: Basic encoding test"
TEST_STRING="hello world"
ENCODED_OUTPUT=$("$BB64" -e <<< "$TEST_STRING" 2>&1 || true)
if [ -n "$ENCODED_OUTPUT" ]; then
    print_test_result "Basic encoding produces output" 0
else
    print_test_result "Basic encoding produces output" 1
fi

# Test 5: Basic decoding test (using -d flag)
echo -e "\nTest 5: Basic decoding test"
# Encode "echo hello" and then decode it
ENCODED_ECHO=$(echo "echo hello" | "$BB64" -e)
if [ -n "$ENCODED_ECHO" ]; then
    DECODED_OUTPUT=$("$BB64" -d "$ENCODED_ECHO" 2>&1 || true)
    if [[ "$DECODED_OUTPUT" == *"echo hello"* ]]; then
        print_test_result "Basic decoding works correctly" 0
    else
        print_test_result "Basic decoding works correctly" 1
        echo "  Expected to contain 'echo hello', got: '$DECODED_OUTPUT'"
    fi
else
    print_test_result "Basic decoding works correctly" 1
    echo "  Failed to encode test string"
fi

cleanup

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

if [ "$TESTS_FAILED" -eq 0 ]; then
    echo -e "\n${GREEN}All tests passed!${NC}"
    exit 0
else
    echo -e "\n${RED}Some tests failed!${NC}"
    exit 1
fi