APP_NAME = Pommedoro
BUILD_DIR = .build
RELEASE_BIN = $(BUILD_DIR)/arm64-apple-macosx/release/$(APP_NAME)
APP_BUNDLE = $(BUILD_DIR)/$(APP_NAME).app
CONTENTS = $(APP_BUNDLE)/Contents
MACOS_DIR = $(CONTENTS)/MacOS
RESOURCES_DIR = $(CONTENTS)/Resources

LAUNCH_AGENT_LABEL = com.pommedoro.app
LAUNCH_AGENT_PLIST = $(HOME)/Library/LaunchAgents/$(LAUNCH_AGENT_LABEL).plist

.PHONY: all build bundle install clean run icons install-agent uninstall-agent dmg release

all: bundle

build:
	swift build -c release

icons:
	@echo "Generating icons from SVG..."
	@mkdir -p Resources/Pommedoro.iconset
	@for size in 16 32 64 128 256 512 1024; do \
		rsvg-convert -w $$size -h $$size Resources/boot-logo.svg \
			-o Resources/Pommedoro.iconset/icon_$${size}x$${size}.png; \
	done
	@cp Resources/Pommedoro.iconset/icon_32x32.png Resources/Pommedoro.iconset/icon_16x16@2x.png
	@cp Resources/Pommedoro.iconset/icon_64x64.png Resources/Pommedoro.iconset/icon_32x32@2x.png
	@cp Resources/Pommedoro.iconset/icon_256x256.png Resources/Pommedoro.iconset/icon_128x128@2x.png
	@cp Resources/Pommedoro.iconset/icon_512x512.png Resources/Pommedoro.iconset/icon_256x256@2x.png
	@cp Resources/Pommedoro.iconset/icon_1024x1024.png Resources/Pommedoro.iconset/icon_512x512@2x.png
	@rm -f Resources/Pommedoro.iconset/icon_64x64.png Resources/Pommedoro.iconset/icon_1024x1024.png
	@iconutil -c icns Resources/Pommedoro.iconset -o Resources/Pommedoro.icns
	@rm -rf Resources/Pommedoro.iconset
	@echo "Icon generated: Resources/Pommedoro.icns"

bundle: build
	@echo "Creating $(APP_NAME).app bundle..."
	@rm -rf $(APP_BUNDLE)
	@mkdir -p $(MACOS_DIR)
	@mkdir -p $(RESOURCES_DIR)
	@cp $(RELEASE_BIN) $(MACOS_DIR)/$(APP_NAME)
	@cp Resources/Info.plist $(CONTENTS)/Info.plist
	@cp Resources/$(APP_NAME).icns $(RESOURCES_DIR)/$(APP_NAME).icns
	@echo "Ad-hoc signing $(APP_BUNDLE)..."
	@codesign --force --deep --sign - \
		--entitlements Resources/Pommedoro.entitlements \
		--options runtime \
		$(APP_BUNDLE)
	@echo "Built $(APP_BUNDLE)"

install: bundle
	@echo "Installing to /Applications..."
	@rm -rf /Applications/$(APP_NAME).app
	@cp -R $(APP_BUNDLE) /Applications/$(APP_NAME).app
	@xattr -cr /Applications/$(APP_NAME).app
	@mkdir -p "$(HOME)/Library/Application Support/Pommedoro"
	@echo "dev" > "$(HOME)/Library/Application Support/Pommedoro/current.sha256"
	@echo "Installed /Applications/$(APP_NAME).app (dev build – auto-update disabled)"

run: bundle
	@open $(APP_BUNDLE)

install-agent: install
	@echo "Installing LaunchAgent..."
	@mkdir -p $(HOME)/Library/LaunchAgents
	@/usr/libexec/PlistBuddy -c "Clear dict" /dev/null 2>/dev/null; \
	printf '<?xml version="1.0" encoding="UTF-8"?>\n\
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n\
<plist version="1.0">\n\
<dict>\n\
\t<key>Label</key>\n\
\t<string>$(LAUNCH_AGENT_LABEL)</string>\n\
\t<key>Program</key>\n\
\t<string>/Applications/$(APP_NAME).app/Contents/MacOS/$(APP_NAME)</string>\n\
\t<key>RunAtLoad</key>\n\
\t<true/>\n\
\t<key>KeepAlive</key>\n\
\t<false/>\n\
</dict>\n\
</plist>\n' > $(LAUNCH_AGENT_PLIST)
	@echo "LaunchAgent installed: $(LAUNCH_AGENT_PLIST)"

uninstall-agent:
	@echo "Removing LaunchAgent..."
	@rm -f $(LAUNCH_AGENT_PLIST)
	@echo "LaunchAgent removed"

dmg: bundle
	@echo "Creating DMG..."
	@rm -f $(BUILD_DIR)/$(APP_NAME).dmg
	@mkdir -p $(BUILD_DIR)/dmg-staging
	@cp -R $(APP_BUNDLE) $(BUILD_DIR)/dmg-staging/
	@ln -s /Applications $(BUILD_DIR)/dmg-staging/Applications
	@cp Resources/install.command $(BUILD_DIR)/dmg-staging/Install.command
	@chmod +x $(BUILD_DIR)/dmg-staging/Install.command
	@cp Resources/$(APP_NAME).icns $(BUILD_DIR)/dmg-staging/.VolumeIcon.icns
	@SetFile -a C $(BUILD_DIR)/dmg-staging
	@hdiutil create -volname "$(APP_NAME)" \
		-srcfolder $(BUILD_DIR)/dmg-staging \
		-ov -format UDZO \
		$(BUILD_DIR)/$(APP_NAME).dmg
	@rm -rf $(BUILD_DIR)/dmg-staging
	@echo "Created $(BUILD_DIR)/$(APP_NAME).dmg"

clean:
	swift package clean
	rm -rf $(APP_BUNDLE)

release: dmg
	@echo "Creating release artifacts..."
	@mkdir -p releases
	@cp $(BUILD_DIR)/$(APP_NAME).dmg releases/$(APP_NAME).dmg
	@shasum -a 256 releases/$(APP_NAME).dmg | awk '{print $$1}' > releases/$(APP_NAME).dmg.sha256
	@echo "SHA256: $$(cat releases/$(APP_NAME).dmg.sha256)"
	@echo "Release artifacts in releases/"
