# SPDX-License-Identifier: Apache-2.0
# Copyright 2025 Longest Valid Parentheses Solution
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -Wpedantic -O2 -g
LDFLAGS = 

# Source files
SOURCES = LongestValidParentheses.cc test_longest_valid_parentheses.cc
OBJECTS = $(SOURCES:.cc=.o)
HEADERS = LongestValidParentheses.h

# Target executable
TARGET = longest_valid_parentheses

# Default target
all: $(TARGET)

# Build the main executable
$(TARGET): $(OBJECTS)
	$(CXX) $(OBJECTS) -o $(TARGET) $(LDFLAGS)

# Compile source files to object files
%.o: %.cc $(HEADERS)
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Run the program
run: $(TARGET)
	./$(TARGET)

# Run tests only (non-interactive)
test: $(TARGET)
	echo "" | ./$(TARGET)

# Clean build artifacts
clean:
	rm -f $(OBJECTS) $(TARGET)

# Debug build with additional flags
debug: CXXFLAGS += -DDEBUG -fsanitize=address -fsanitize=undefined
debug: LDFLAGS += -fsanitize=address -fsanitize=undefined
debug: $(TARGET)

# Release build with optimizations
release: CXXFLAGS = -std=c++17 -Wall -Wextra -Wpedantic -O3 -DNDEBUG
release: $(TARGET)

# Static analysis with cppcheck (if available)
analyze:
	@if command -v cppcheck >/dev/null 2>&1; then \
		cppcheck --enable=all --std=c++17 $(SOURCES) $(HEADERS); \
	else \
		echo "cppcheck not found, skipping static analysis"; \
	fi

# Format code with clang-format (if available)
format:
	@if command -v clang-format >/dev/null 2>&1; then \
		clang-format -i $(SOURCES) $(HEADERS); \
		echo "Code formatted successfully"; \
	else \
		echo "clang-format not found, skipping formatting"; \
	fi

# Install target (optional)
install: $(TARGET)
	@echo "Installing $(TARGET) to /usr/local/bin/"
	@sudo cp $(TARGET) /usr/local/bin/

# Uninstall target (optional)
uninstall:
	@echo "Removing $(TARGET) from /usr/local/bin/"
	@sudo rm -f /usr/local/bin/$(TARGET)

# Help target
help:
	@echo "Available targets:"
	@echo "  all      - Build the program (default)"
	@echo "  run      - Build and run the program"
	@echo "  test     - Build and run automated tests"
	@echo "  clean    - Remove build artifacts"
	@echo "  debug    - Build with debug flags and sanitizers"
	@echo "  release  - Build optimized release version"
	@echo "  analyze  - Run static analysis (requires cppcheck)"
	@echo "  format   - Format code (requires clang-format)"
	@echo "  install  - Install to /usr/local/bin"
	@echo "  uninstall- Remove from /usr/local/bin"
	@echo "  help     - Show this help message"

# Declare phony targets
.PHONY: all run test clean debug release analyze format install uninstall help