.PHONY: all clean test install help dev tikker plot process merge index popular

CC := gcc
CFLAGS := -Ofast -Wall -Wextra
DEBUG_FLAGS := -g -O0 -DDEBUG
LDFLAGS := -lsqlite3 -lpthread

INSTALL_PREFIX ?= /usr/local
BUILD_DIR := ./build
BIN_DIR := $(BUILD_DIR)/bin

PYTHON := python3
VENV := .venv
VENV_PYTHON := $(VENV)/bin/python

all: tikker
	@echo "Build complete"

help:
	@echo "Tikker Build System"
	@echo ""
	@echo "Build Targets:"
	@echo "  make tikker       - Build tikker binary (default)"
	@echo "  make all          - Same as tikker"
	@echo "  make dev          - Build with debug symbols"
	@echo "  make clean        - Remove build artifacts"
	@echo "  make install      - Install binary to $(INSTALL_PREFIX)/bin"
	@echo ""
	@echo "Python Targets:"
	@echo "  make ensure_env   - Create Python virtual environment"
	@echo "  make plot         - Generate graphs and reports"
	@echo "  make process      - Run processing pipeline"
	@echo "  make merge        - Merge visualization outputs"
	@echo "  make index        - Index words in database"
	@echo "  make popular      - Show popular typed words"
	@echo ""
	@echo "Tikker Commands:"
	@echo "  ./tikker                   - Start keyboard monitoring"
	@echo "  ./tikker presses_today     - Show today's keystrokes"
	@echo "  ./tikker stats daily       - Daily statistics"
	@echo "  ./tikker stats hourly      - Hourly breakdown"
	@echo "  ./tikker stats weekly      - Weekly statistics"
	@echo "  ./tikker stats weekday     - Weekday comparison"
	@echo "  ./tikker stats top-keys N  - Top N keys"
	@echo "  ./tikker stats top-words N - Top N words"
	@echo "  ./tikker stats summary     - Overall summary"
	@echo "  ./tikker decode FILE       - Decode keystroke log"
	@echo "  ./tikker help              - Show usage"

$(BIN_DIR):
	@mkdir -p $(BIN_DIR)

tikker: $(BIN_DIR)
	@echo "Building tikker..."
	@$(CC) $(CFLAGS) tikker.c -o $(BIN_DIR)/tikker $(LDFLAGS)
	@cp $(BIN_DIR)/tikker ./tikker
	@echo "Built: ./tikker"

dev: CFLAGS := $(DEBUG_FLAGS)
dev: clean tikker
	@echo "Debug build complete"

ensure_env:
	@if [ ! -d "$(VENV)" ]; then \
		$(PYTHON) -m venv $(VENV); \
		$(VENV_PYTHON) -m pip install --upgrade pip; \
		$(VENV_PYTHON) -m pip install matplotlib requests fastapi uvicorn openai dataset; \
		echo "Virtual environment created at $(VENV)"; \
	else \
		echo "Virtual environment already exists"; \
	fi

plot: ensure_env
	@PYTHONPATH=/home/retoor/bin $(VENV_PYTHON) plot.py
	@$(VENV_PYTHON) merge.py

process: ensure_env
	@PYTHONPATH=/home/retoor/bin $(VENV_PYTHON) process.py

merge: ensure_env
	@$(VENV_PYTHON) merge.py

index: ensure_env
	@$(VENV_PYTHON) tags.py --index

popular: ensure_env
	@$(VENV_PYTHON) tags.py --popular

install: tikker
	@echo "Installing to $(INSTALL_PREFIX)/bin..."
	@mkdir -p $(INSTALL_PREFIX)/bin
	@install -m 755 $(BIN_DIR)/tikker $(INSTALL_PREFIX)/bin/
	@echo "Installed: $(INSTALL_PREFIX)/bin/tikker"

clean:
	@echo "Cleaning build artifacts..."
	@rm -rf $(BUILD_DIR)
	@rm -f tikker
	@find . -name "*.o" -delete
	@find . -name "*.gcov" -delete
	@find . -name "*.gcda" -delete
	@find . -name "*.gcno" -delete
	@echo "Clean complete"
