SWIFT_PACKAGE_NAME = MacSync
SOURCES_CLIENT = src/client @packages
SWIFTLINT_CONFIG = .swiftlint.yml

-include $(HOME)/Code/@packages/@swift/makefiles/base.mk

ifndef SWIFTLINT
SWIFTLINT := $(shell command -v swiftlint 2>/dev/null)
endif
ifndef SWIFTFORMAT
SWIFTFORMAT := $(shell command -v swiftformat 2>/dev/null)
endif

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 help

all: build

help:
	@echo "$(COLOR_GREEN)MacSync$(COLOR_NC)"
	@echo ""
	@echo "  build         - Build all targets (debug)"
	@echo "  build-release - Build all targets (release)"
	@echo "  test          - Run tests"
	@echo "  test-remote   - Run tests on plum via SSH"
	@echo "  lint          - Run SwiftLint"
	@echo "  format        - Run SwiftFormat"
	@echo "  clean         - Clean build artifacts"
	@echo "  run           - Build and run MacSyncApp"
	@echo ""

lint:
	@echo "$(COLOR_GREEN)Linting...$(COLOR_NC)"
ifdef SWIFTLINT
	@$(SWIFTLINT) lint --config $(SWIFTLINT_CONFIG) $(SOURCES_CLIENT) --strict
else
	@echo "$(COLOR_YELLOW)SwiftLint not installed$(COLOR_NC)"; exit 1
endif

lint-fix:
	@echo "$(COLOR_GREEN)Linting (fix)...$(COLOR_NC)"
ifdef SWIFTLINT
	@$(SWIFTLINT) lint --fix $(SOURCES_CLIENT)
else
	@echo "$(COLOR_YELLOW)SwiftLint not installed$(COLOR_NC)"
endif

format:
	@echo "$(COLOR_GREEN)Formatting...$(COLOR_NC)"
ifdef SWIFTFORMAT
	@$(SWIFTFORMAT) $(SOURCES_CLIENT)
else
	@echo "$(COLOR_YELLOW)SwiftFormat not installed$(COLOR_NC)"
endif

build:
	@echo "$(COLOR_GREEN)Building $(SWIFT_PACKAGE_NAME) (debug)...$(COLOR_NC)"
	@swift build --product MacSyncApp

build-release:
	@echo "$(COLOR_GREEN)Building $(SWIFT_PACKAGE_NAME) (release)...$(COLOR_NC)"
	@swift build -c release --product MacSyncApp

clean:
	@swift package clean
	@rm -rf .build

test:
	@echo "$(COLOR_GREEN)Running tests...$(COLOR_NC)"
	@swift test

test-remote:
	@./deploy/test-remote.sh

run: build
	@.build/debug/MacSyncApp
