# Portfolio Journal Makefile
# Usage: make [target]
#
# Available targets:
#   test          - Run all unit tests
#   test-unit     - Run unit tests only
#   test-ui       - Run UI tests only
#   test-coverage - Run tests with code coverage
#   build         - Build the app for debug
#   build-release - Build the app for release
#   clean         - Clean build artifacts
#   setup-tests   - Set up test targets in Xcode project
#   help          - Show this help message

.PHONY: test test-unit test-ui test-coverage build build-release clean setup-tests help

# Configuration
PROJECT = PortfolioJournal.xcodeproj
SCHEME = PortfolioJournal
DEVICE = iPhone 17
DESTINATION = platform=iOS Simulator,name=$(DEVICE)

# Default target
.DEFAULT_GOAL := help

# Help
help:
	@echo "Portfolio Journal - Build & Test Commands"
	@echo ""
	@echo "Usage: make [target]"
	@echo ""
	@echo "Test targets:"
	@echo "  test            Run all unit tests"
	@echo "  test-unit       Run unit tests only"
	@echo "  test-ui         Run UI tests only"
	@echo "  test-coverage   Run tests with code coverage"
	@echo "  test-quick      Run tests without pretty output (faster)"
	@echo ""
	@echo "Build targets:"
	@echo "  build           Build the app for debug"
	@echo "  build-release   Build the app for release"
	@echo "  clean           Clean build artifacts"
	@echo ""
	@echo "Setup targets:"
	@echo "  setup-tests     Set up test targets in Xcode project"
	@echo "  install-tools   Install required development tools"
	@echo ""
	@echo "Examples:"
	@echo "  make test                    # Run all tests"
	@echo "  make test DEVICE='iPhone 16' # Run tests on specific device"

# Run all tests
test:
	@echo "Running all tests on $(DEVICE)..."
	@./Scripts/run_tests.sh --unit --device "$(DEVICE)"

# Run unit tests only
test-unit:
	@echo "Running unit tests on $(DEVICE)..."
	@./Scripts/run_tests.sh --unit --device "$(DEVICE)"

# Run UI tests only
test-ui:
	@echo "Running UI tests on $(DEVICE)..."
	@./Scripts/run_tests.sh --ui --device "$(DEVICE)"

# Run tests with coverage
test-coverage:
	@echo "Running tests with coverage on $(DEVICE)..."
	@./Scripts/run_tests.sh --all --coverage --device "$(DEVICE)"

# Quick test run without xcpretty
test-quick:
	@echo "Running quick tests on $(DEVICE)..."
	xcodebuild test \
		-project $(PROJECT) \
		-scheme $(SCHEME) \
		-destination "$(DESTINATION)" \
		-only-testing:PortfolioJournalTests \
		| grep -E "(Test Case|passed|failed|error:)" || true

# Build for debug
build:
	@echo "Building for Debug..."
	xcodebuild \
		-project $(PROJECT) \
		-scheme $(SCHEME) \
		-configuration Debug \
		-destination "$(DESTINATION)" \
		build

# Build for release
build-release:
	@echo "Building for Release..."
	xcodebuild \
		-project $(PROJECT) \
		-scheme $(SCHEME) \
		-configuration Release \
		build

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	xcodebuild \
		-project $(PROJECT) \
		-scheme $(SCHEME) \
		clean
	rm -rf ~/Library/Developer/Xcode/DerivedData/PortfolioJournal-*

# Set up test targets
setup-tests:
	@echo "Setting up test targets..."
	@if command -v ruby >/dev/null 2>&1; then \
		ruby Scripts/setup_tests.rb; \
	else \
		echo "Ruby not found. Please add test target manually in Xcode:"; \
		echo "1. File > New > Target > iOS Unit Testing Bundle"; \
		echo "2. Name it 'PortfolioJournalTests'"; \
		echo "3. Add test files from PortfolioJournalTests folder"; \
	fi

# Install development tools
install-tools:
	@echo "Installing development tools..."
	@if command -v gem >/dev/null 2>&1; then \
		gem install xcpretty xcodeproj; \
	else \
		echo "RubyGems not found. Please install Ruby first."; \
	fi

# Pre-release checks
pre-release: clean build-release test
	@echo ""
	@echo "✅ Pre-release checks passed!"
	@echo "Ready to submit to App Store."
