CC = gcc
CFLAGS = -Wall -Wextra -O2 -Isrc
LDFLAGS = -lm -lpthread

SRC_DIR = src
TEST_DIR = tests
EXAMPLE_DIR = examples
BUILD_DIR = build
BIN_DIR = bin

TARGET = $(BIN_DIR)/rc

SOURCES = $(SRC_DIR)/main.c \
          $(SRC_DIR)/globals.c \
          $(SRC_DIR)/tokenizer.c \
          $(SRC_DIR)/parser.c \
          $(SRC_DIR)/interpreter.c \
          $(SRC_DIR)/native_functions.c \
          $(SRC_DIR)/string_utils.c \
          $(SRC_DIR)/preprocessor.c \
          $(SRC_DIR)/oop.c \
          $(SRC_DIR)/error.c \
          $(SRC_DIR)/stdlib/stdlib.c \
          $(SRC_DIR)/stdlib/string/string_stdlib.c \
          $(SRC_DIR)/stdlib/math/math_stdlib.c \
          $(SRC_DIR)/stdlib/io/file_stdlib.c \
          $(SRC_DIR)/stdlib/io/socket_stdlib.c \
          $(SRC_DIR)/stdlib/async/async_stdlib.c \
          $(SRC_DIR)/stdlib/constants/constants_stdlib.c \
          $(SRC_DIR)/stdlib/eval/eval_stdlib.c

OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)

TESTS = $(TEST_DIR)/unittest.rc \
        $(TEST_DIR)/test_language_features.rc \
        $(TEST_DIR)/test_stdlib_string.rc \
        $(TEST_DIR)/test_stdlib_math.rc \
        $(TEST_DIR)/test_stdlib_io.rc \
        $(TEST_DIR)/test_stdlib_async.rc \
        $(TEST_DIR)/test_oop.rc \
        $(TEST_DIR)/test_preprocessor.rc \
        $(TEST_DIR)/test_eval.rc \
        $(TEST_DIR)/test_input_validation.rc \
        $(TEST_DIR)/run_all_tests.rc

EXAMPLES = $(EXAMPLE_DIR)/http_simple.rc \
           $(EXAMPLE_DIR)/http_persistent.rc \
           $(EXAMPLE_DIR)/http_multi.rc \
           $(EXAMPLE_DIR)/async_demo.rc

.PHONY: all clean test run-tests run-examples help

all: dirs $(TARGET)

dirs:
	@mkdir -p $(BUILD_DIR) $(BIN_DIR)
	@mkdir -p $(BUILD_DIR)/stdlib/string
	@mkdir -p $(BUILD_DIR)/stdlib/math
	@mkdir -p $(BUILD_DIR)/stdlib/io
	@mkdir -p $(BUILD_DIR)/stdlib/async
	@mkdir -p $(BUILD_DIR)/stdlib/constants
	@mkdir -p $(BUILD_DIR)/stdlib/eval

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

$(TARGET): dirs $(OBJECTS)
	$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)

clean:
	rm -rf $(BUILD_DIR) $(BIN_DIR)

test: $(TARGET)
	@echo "================================================================"
	@echo "         RC LANGUAGE - COMPREHENSIVE TEST SUITE"
	@echo "================================================================"
	@echo ""
	@echo "[1/9] Running Language Features Tests..."
	@$(TARGET) $(TEST_DIR)/test_language_features.rc
	@echo ""
	@echo "[2/9] Running String Standard Library Tests..."
	@$(TARGET) $(TEST_DIR)/test_stdlib_string.rc
	@echo ""
	@echo "[3/9] Running Math Standard Library Tests..."
	@$(TARGET) $(TEST_DIR)/test_stdlib_math.rc
	@echo ""
	@echo "[4/9] Running I/O Standard Library Tests..."
	@$(TARGET) $(TEST_DIR)/test_stdlib_io.rc
	@echo ""
	@echo "[5/9] Running Async Standard Library Tests..."
	@$(TARGET) $(TEST_DIR)/test_stdlib_async.rc
	@echo ""
	@echo "[6/9] Running OOP Features Tests..."
	@$(TARGET) $(TEST_DIR)/test_oop.rc
	@echo ""
	@echo "[7/9] Running Preprocessor Tests..."
	@$(TARGET) $(TEST_DIR)/test_preprocessor.rc
	@echo ""
	@echo "[8/9] Running Eval Function Tests..."
	@$(TARGET) $(TEST_DIR)/test_eval.rc
	@echo ""
	@echo "[9/9] Running Input Validation Tests..."
	@$(TARGET) $(TEST_DIR)/test_input_validation.rc
	@echo ""
	@echo "================================================================"
	@echo "All comprehensive tests completed!"
	@echo "================================================================"

test-smoke: $(TARGET)
	@$(TARGET) $(TEST_DIR)/run_all_tests.rc

test-language: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_language_features.rc

test-string: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_stdlib_string.rc

test-math: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_stdlib_math.rc

test-io: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_stdlib_io.rc

test-async: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_stdlib_async.rc

test-oop: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_oop.rc

test-preprocessor: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_preprocessor.rc

test-eval: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_eval.rc

test-validation: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_input_validation.rc

test-robustness: $(TARGET)
	$(TARGET) $(TEST_DIR)/test_robustness.rc

demo-error-stacktrace: $(TARGET)
	@echo "=== Demonstrating Full Stack Trace ==="
	-$(TARGET) $(TEST_DIR)/test_error_stacktrace.rc

demo-error-undefined: $(TARGET)
	@echo "=== Demonstrating Undefined Variable Error ==="
	-$(TARGET) $(TEST_DIR)/test_error_undefined_var.rc

demo-error-native: $(TARGET)
	@echo "=== Demonstrating Error in Native Function Call ==="
	-$(TARGET) $(TEST_DIR)/test_error_native.rc

demo-errors: demo-error-stacktrace demo-error-undefined demo-error-native

run-http-simple: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/http_simple.rc

run-http-persistent: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/http_persistent.rc

run-http-multi: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/http_multi.rc

run-async-demo: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/async_demo.rc

help:
	@echo "RC - Retoor's C Interpreter"
	@echo ""
	@echo "Usage:"
	@echo "  make                  - Build the interpreter"
	@echo "  make test             - Run ALL comprehensive tests (178+ tests)"
	@echo "  make test-smoke       - Run quick smoke test (31 tests)"
	@echo "  make clean            - Remove all build artifacts"
	@echo ""
	@echo "Test Categories (organized by src/ structure):"
	@echo "  make test-language    - Language features (63 tests)"
	@echo "  make test-string      - String stdlib (31 tests)"
	@echo "  make test-math        - Math stdlib (26 tests)"
	@echo "  make test-io          - I/O stdlib (comprehensive)"
	@echo "  make test-async       - Async stdlib (20+ tests)"
	@echo "  make test-oop         - OOP features (31 tests)"
	@echo "  make test-preprocessor - Preprocessor (2 tests)"
	@echo "  make test-eval        - Eval function (30 tests)"
	@echo "  make test-validation  - Input validation (77 tests)"
	@echo "  make test-robustness  - Robustness and safety (25 tests)"
	@echo ""
	@echo "Error Handling Demonstrations:"
	@echo "  make demo-errors              - Run all error handling demos"
	@echo "  make demo-error-stacktrace    - Show full stack trace with file:line"
	@echo "  make demo-error-undefined     - Show undefined variable error with tips"
	@echo "  make demo-error-native        - Show error during native function call"
	@echo ""
	@echo "Examples:"
	@echo "  make run-http-simple            - Run http_simple.rc (single connection HTTP server)"
	@echo "  make run-http-persistent        - Run http_persistent.rc (persistent HTTP server)"
	@echo "  make run-http-multi             - Run http_multi.rc (HTTP server, 100 connections)"
	@echo "  make run-async-demo             - Run async_demo.rc (comprehensive async I/O demo)"
	@echo ""
	@echo "Directory Structure:"
	@echo "  src/              - Source code files"
	@echo "    stdlib/string/  - String functions"
	@echo "    stdlib/math/    - Math functions"
	@echo "    stdlib/io/      - I/O functions"
	@echo "    stdlib/async/   - Async functions"
	@echo "    stdlib/eval/    - Eval function"
	@echo "  tests/            - Test programs (unittest framework)"
	@echo "  examples/         - Example programs (.rc files)"
	@echo "  build/            - Compiled object files"
	@echo "  bin/              - Final executable (rc)"
