.PHONY: all pack clean test doc run stddev help

# Team logins (from README.md)
TEAM_LOGINS = xhronsj00_xbajtaa00_xkostej00_xprsekd00
SUBMISSION_DIR = $(TEAM_LOGINS)
SUBMISSION_ARCHIVE = $(TEAM_LOGINS).zip

# Default target
all: install-deps

# Install Python dependencies from requirements.txt
install-deps:
	pip3 install -r requirements.txt

# Run unit tests for the math library
test:
	python3 -m pytest calc_tests.py -v 2>/dev/null || python3 -m unittest discover -s . -p "calc_tests.py" -v

# Generate documentation using Doxygen
doc:
	@DOXYGEN_PATH=""; \
	if command -v doxygen >/dev/null 2>&1; then \
		DOXYGEN_PATH="doxygen"; \
	elif [ -x "/usr/local/bin/doxygen" ]; then \
		DOXYGEN_PATH="/usr/local/bin/doxygen"; \
	elif [ -x "/opt/homebrew/bin/doxygen" ]; then \
		DOXYGEN_PATH="/opt/homebrew/bin/doxygen"; \
	fi; \
	if [ -n "$$DOXYGEN_PATH" ]; then \
		$$DOXYGEN_PATH Doxyfile; \
		echo "✓ API documentation generated successfully"; \
	else \
		echo "⚠ WARNING: doxygen is not installed or not found in PATH"; \
		echo "  Documentation will not be generated."; \
		echo "  To install doxygen:"; \
		echo "    macOS:   brew install doxygen"; \
		echo "    Ubuntu:  sudo apt-get install doxygen"; \
		echo ""; \
	fi

# Run the main calculator application
run: install-deps
	python3 -m application

# Prepare and run the standard deviation profiling program
# Python script - just ensure dependencies are installed
stddev: install-deps
	@echo "Running stddev program for profiling..."
	@echo "Usage: python3 stddev.py < input.txt"
	@echo "Or: cat data.txt | python3 stddev.py"

# Package the project for submission
# Creates structure: xlogin01_xlogin02_xlogin03_xlogin04.zip containing:
#   - doc/        (generated API documentation)
#   - install/    (installers and uninstallers)
#   - repo/       (full git repository with history)
pack: doc
	@echo "Creating submission package in correct format..."
	@# Clean any previous submission artifacts
	@rm -rf /tmp/$(SUBMISSION_DIR) 2>/dev/null || true
	@rm -f ../$(SUBMISSION_ARCHIVE) 2>/dev/null || true

	@# Create main submission directory
	@mkdir -p /tmp/$(SUBMISSION_DIR)

	@# Create subdirectories
	@mkdir -p /tmp/$(SUBMISSION_DIR)/doc
	@mkdir -p /tmp/$(SUBMISSION_DIR)/install
	@mkdir -p /tmp/$(SUBMISSION_DIR)/repo

	@# 1. Copy generated documentation (Doxygen generates html and latex directories)
	@if [ -d html ] || [ -d latex ]; then \
		[ -d html ] && cp -r html /tmp/$(SUBMISSION_DIR)/doc/ 2>/dev/null || true; \
		[ -d latex ] && cp -r latex /tmp/$(SUBMISSION_DIR)/doc/ 2>/dev/null || true; \
		echo "✓ Documentation copied to submission package"; \
	else \
		echo "⚠ Warning: html and/or latex directories not found. Run 'make doc' first."; \
	fi

	@# 2. Copy installers and uninstallers
	@if [ -d ../install ]; then \
		cp -r ../install/* /tmp/$(SUBMISSION_DIR)/install/; \
	else \
		echo "Warning: install folder not found."; \
	fi

	@# 3. Copy full repository with .git history and all content
	@cp -r ../.git /tmp/$(SUBMISSION_DIR)/repo/.git
	@cp -r ../src /tmp/$(SUBMISSION_DIR)/repo/src
	@rm -rf /tmp/$(SUBMISSION_DIR)/repo/src/html 2>/dev/null || true
	@rm -rf /tmp/$(SUBMISSION_DIR)/repo/src/latex 2>/dev/null || true
	@cp -r ../plan /tmp/$(SUBMISSION_DIR)/repo/plan
	@cp -r ../profiling /tmp/$(SUBMISSION_DIR)/repo/profiling
	@cp -r ../mockup /tmp/$(SUBMISSION_DIR)/repo/mockup
	@cp ../README.md /tmp/$(SUBMISSION_DIR)/repo/
	@cp ../LICENSE /tmp/$(SUBMISSION_DIR)/repo/
	@cp ../.gitignore /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../.editorconfig /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../manual.pdf /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../screenshot.png /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../screenshot.jpg /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../debugging.png /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../debugging.jpg /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../debugging.pdf /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../skutecnost.txt /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true
	@cp ../hodnoceni.txt /tmp/$(SUBMISSION_DIR)/repo/ 2>/dev/null || true

	@# Create the ZIP archive
	@cd /tmp && zip -r -q $(SUBMISSION_ARCHIVE) $(SUBMISSION_DIR)
	@mv /tmp/$(SUBMISSION_ARCHIVE) ../$(SUBMISSION_ARCHIVE)
	@rm -rf /tmp/$(SUBMISSION_DIR)

	@echo ""
	@echo "✓✓✓ Submission package created successfully ✓✓✓"
	@echo ""
	@echo "Archive: $(SUBMISSION_ARCHIVE)"
	@echo "Directory structure inside archive:"
	@echo "  $(SUBMISSION_DIR)/"
	@echo "  ├── doc/               (generated API documentation)"
	@echo "  ├── install/           (installers for calculator and profiling)"
	@echo "  └── repo/              (complete repository with .git history)"
	@echo ""
	@echo "This archive is ready for submission to:"
	@echo "  /ivs-proj2/odevzdane/xhronsj00/"

# Clean build artifacts and temporary files
clean:
	find .. -type d -name '__pycache__' -exec rm -rf {} + 2>/dev/null || true
	find .. -type f -name '*.pyc' -delete 2>/dev/null || true
	find .. -type d -name '.pytest_cache' -exec rm -rf {} + 2>/dev/null || true
	find .. -type d \( -name 'html' -o -name 'latex' \) -exec rm -rf {} + 2>/dev/null || true
	rm -rf ../IVS_projekt2.tar.gz 2>/dev/null || true
	rm -rf ../$(SUBMISSION_ARCHIVE) 2>/dev/null || true

# Display help information
help:
	@echo "=========================================="
	@echo "IVS Projekt 2 - Calculator Application"
	@echo "Makefile Targets - GNU Make"
	@echo "=========================================="
	@echo ""
	@echo "Usage: make [target]"
	@echo "       make (without target executes 'all')"
	@echo ""
	@echo "Available targets:"
	@echo ""
	@echo "  all       Install Python dependencies (PyQt5) from requirements.txt"
	@echo "            Run this first on fresh VM installation"
	@echo "            DEFAULT TARGET: automatically executed when running 'make'"
	@echo ""
	@echo "  install-deps"
	@echo "            Install all Python dependencies listed in requirements.txt"
	@echo ""
	@echo "  run       Run the calculator GUI application"
	@echo "            Requires: 'make all' to be run first for dependencies"
	@echo ""
	@echo "  test      Run unit tests for the mathematical library"
	@echo "            Tests located in: calc_tests.py"
	@echo ""
	@echo "  doc       Generate API documentation using Doxygen"
	@echo "            Configuration file: Doxyfile"
	@echo "            Output: html/ and latex/ directories"
	@echo "            NOTE: Requires doxygen to be installed"
	@echo "                  Install: brew install doxygen (macOS)"
	@echo "                            sudo apt-get install doxygen (Ubuntu)"
	@echo ""
	@echo "  stddev    Prepare the standard deviation profiling program"
	@echo "            Usage: python3 stddev.py < data.txt"
	@echo "            Calculates sample standard deviation from input numbers"
	@echo ""
	@echo "  pack      Create submission archive ($(SUBMISSION_ARCHIVE))"
	@echo "            Runs 'make doc' to generate documentation first"
	@echo "            Creates proper submission structure with:"
	@echo "              - doc/      (generated API documentation)"
	@echo "              - install/  (installers and uninstallers)"
	@echo "              - repo/     (complete git repository with history)"
	@echo "            Archive is ready for submission to:"
	@echo "              /ivs-proj2/odevzdane/xhronsj00/"
	@echo ""
	@echo "  clean     Remove all generated files and temporary artifacts"
	@echo "            Removes: __pycache__, *.pyc, .pytest_cache,"
	@echo "            Doxygen output, build archives"
	@echo ""
	@echo "  help      Display this help message"
	@echo ""
	@echo "=========================================="
	@echo "Quick Start on Virtual Machine:"
	@echo "=========================================="
	@echo "1. make all        (install dependencies)"
	@echo "2. make run        (start the calculator)"
	@echo "3. make test       (run unit tests)"
	@echo "4. make doc        (generate documentation)"
	@echo "5. make pack       (create submission archive)"
	@echo ""
	@echo "File created: xhronsj00_xbajtaa00_xkostej00_xprsekd00.zip"
	@echo "=========================================="
	@echo ""
	@echo "Compilation on VM without Makefile:"
	@echo "  cd src/"
	@echo "  pip3 install -r requirements.txt"
	@echo "  python3 -m application"
	@echo ""
	@echo "=========================================="
