# Makefile for uni-json nested manipulation example
#
# Copyright (C) 2025 VDev Project
# MIT-licensed.

# Programs
CC := gcc
CFLAGS := -Wall -Wextra -g -O2 -I../../include

# Directories
UNI_JSON_DIR := ../..
BUILD_DIR := build

# Files
NESTED_SRC := nested_example.c
CBINDINGS_SRC := nested_bindings.c
NESTED_OBJ := $(BUILD_DIR)/nested_example.o
CBINDINGS_OBJ := $(BUILD_DIR)/nested_bindings.o
NESTED_TARGET := $(BUILD_DIR)/nested_example

# Find uni-json library
UNI_JSON_LIB := $(UNI_JSON_DIR)/bin/libuni-json.a
UNI_JSON_SO := $(UNI_JSON_DIR)/bin/libuni-json.so

.PHONY: all clean

all: $(NESTED_TARGET)

# Create build directory
$(BUILD_DIR):
	mkdir -p $@

# Build uni-json library if needed
$(UNI_JSON_LIB):
	$(MAKE) -C $(UNI_JSON_DIR) bin/libuni-json.a

# Compile nested bindings
$(CBINDINGS_OBJ): $(CBINDINGS_SRC) | $(BUILD_DIR)
	$(CC) $(CFLAGS) -c $< -o $@

# Compile nested example
$(NESTED_OBJ): $(NESTED_SRC) | $(BUILD_DIR)
	$(CC) $(CFLAGS) -c $< -o $@

# Link final executable
$(NESTED_TARGET): $(NESTED_OBJ) $(CBINDINGS_OBJ) $(UNI_JSON_LIB) | $(BUILD_DIR)
	$(CC) $(CFLAGS) -o $@ $(NESTED_OBJ) $(CBINDINGS_OBJ) $(UNI_JSON_LIB) -lm

# Alternative: Use shared library
$(NESTED_TARGET)-shared: $(NESTED_OBJ) $(CBINDINGS_OBJ) $(UNI_JSON_SO) | $(BUILD_DIR)
	$(CC) $(CFLAGS) -o $@ $(NESTED_OBJ) $(CBINDINGS_OBJ) -L$(UNI_JSON_DIR)/bin -luni-json -lm

clean:
	rm -rf $(BUILD_DIR)

# Run the example
run: $(NESTED_TARGET)
	./$(NESTED_TARGET)

# Install dependencies (build uni-json library)
deps:
	$(MAKE) -C $(UNI_JSON_DIR) bin/libuni-json.a

help:
	@echo "Available targets:"
	@echo "  all          - Build the nested example"
	@echo "  clean        - Remove build files"
	@echo "  run          - Build and run the example"
	@echo "  deps         - Build uni-json library dependency"
	@echo "  help         - Show this help message"
	@echo ""
	@echo "This example demonstrates:"
	@echo "  - Parsing complex nested JSON structures"
	@echo "  - Querying values using dot notation paths"
	@echo "  - Creating and modifying JSON objects"
	@echo "  - Working with arrays and nested objects"
