# Lilith Messaging Agent - Makefile
# Uses shared Swift tooling from ~/Code/@packages/@swift/

SWIFT_PACKAGE_NAME = LilithMessaging
SOURCES = Sources
SWIFTLINT_CONFIG = .swiftlint.yml

# Include shared base if available, otherwise use inline definitions
-include $(HOME)/Code/@packages/@swift/makefiles/base.mk

# Fallback definitions if shared makefile not available
ifndef SWIFTLINT
SWIFTLINT := $(shell command -v swiftlint 2>/dev/null)
endif
ifndef SWIFTFORMAT
SWIFTFORMAT := $(shell command -v swiftformat 2>/dev/null)
endif

# Colors
COLOR_GREEN := \033[0;32m
COLOR_YELLOW := \033[0;33m
COLOR_NC := \033[0m

.PHONY: all lint lint-fix format build build-release clean test test-remote run install help version

# Default target
all: lint build

help:
	@echo "$(COLOR_GREEN)Lilith Messaging Agent$(COLOR_NC)"
	@echo ""
	@echo "Quality:"
	@echo "  lint          - Run SwiftLint"
	@echo "  lint-fix      - Run SwiftLint with auto-fix"
	@echo "  format        - Run SwiftFormat"
	@echo ""
	@echo "  test-remote   - Run tests on plum (MacBook) via SSH"
	@echo ""
	@echo "Build:"
	@echo "  build         - Build debug (auto-generates version)"
	@echo "  build-release - Build release (auto-generates version)"
	@echo "  version       - Regenerate AppVersion.swift from VERSION.json"
	@echo "  clean         - Clean build artifacts"
	@echo ""
	@echo "Run:"
	@echo "  run           - Build and run"
	@echo "  install       - Install to ~/Applications"
	@echo ""

# Lint (fallback if base.mk not included)
ifndef lint
lint:
	@echo "$(COLOR_GREEN)Running SwiftLint...$(COLOR_NC)"
ifdef SWIFTLINT
	@$(SWIFTLINT) lint --config $(SWIFTLINT_CONFIG) $(SOURCES) --strict
else
	@echo "$(COLOR_YELLOW)SwiftLint not installed. Run: brew install swiftlint$(COLOR_NC)"
	@exit 1
endif
endif

# Lint fix (fallback)
ifndef lint-fix
lint-fix:
	@echo "$(COLOR_GREEN)Running SwiftLint with auto-fix...$(COLOR_NC)"
ifdef SWIFTLINT
	@$(SWIFTLINT) lint --config $(SWIFTLINT_CONFIG) --fix $(SOURCES)
else
	@echo "$(COLOR_YELLOW)SwiftLint not installed$(COLOR_NC)"
endif
endif

# Format (fallback)
ifndef format
format:
	@echo "$(COLOR_GREEN)Running SwiftFormat...$(COLOR_NC)"
ifdef SWIFTFORMAT
	@$(SWIFTFORMAT) $(SOURCES)
else
	@echo "$(COLOR_YELLOW)SwiftFormat not installed. Run: brew install swiftformat$(COLOR_NC)"
endif
endif

# Version generation (always runs before build)
version:
	@./generate-version.sh

# Build (fallback)
ifndef build
build: version
	@echo "$(COLOR_GREEN)Building $(SWIFT_PACKAGE_NAME) (debug)...$(COLOR_NC)"
	@swift build -c debug
endif

# Build release (fallback)
ifndef build-release
build-release: version
	@echo "$(COLOR_GREEN)Building $(SWIFT_PACKAGE_NAME) (release)...$(COLOR_NC)"
	@swift build -c release
endif

# Clean (fallback)
ifndef clean
clean:
	@echo "$(COLOR_GREEN)Cleaning...$(COLOR_NC)"
	@swift package clean
	@rm -rf .build DerivedData
endif

# Test (fallback)
ifndef test
test:
	@echo "$(COLOR_GREEN)Running tests...$(COLOR_NC)"
	@swift test
endif

# Remote test (runs on plum over SSH)
test-remote:
	@./test-remote.sh

# Project-specific targets
run: build
	@echo "$(COLOR_GREEN)Running $(SWIFT_PACKAGE_NAME)...$(COLOR_NC)"
	@.build/debug/$(SWIFT_PACKAGE_NAME)

install: build-release
	@echo "$(COLOR_GREEN)Installing via install.sh...$(COLOR_NC)"
	@./install.sh
