APP_NAME    := tusk
VERSION     := $(shell grep '"version"' src-tauri/tauri.conf.json | head -1 | sed 's/.*: "//;s/".*//')
PREFIX      := /usr/local
BINDIR      := $(PREFIX)/bin
DATADIR     := $(PREFIX)/share
BUNDLE_DIR  := src-tauri/target/release/bundle

# Rust cross-compilation target (override: make build TARGET=aarch64-unknown-linux-gnu)
TARGET      :=
TARGET_FLAG := $(if $(TARGET),--target $(TARGET),)
TARGET_DIR  := $(if $(TARGET),src-tauri/target/$(TARGET)/release,src-tauri/target/release)

# ──────────────────────────────────────────────
#  Development
# ──────────────────────────────────────────────

.PHONY: dev
dev: ## Run app in dev mode (Vite HMR + Rust backend)
	npm run tauri dev

.PHONY: dev-frontend
dev-frontend: node_modules ## Run only the Vite frontend dev server
	npm run dev

.PHONY: check
check: ## Check Rust compilation without building
	cd src-tauri && cargo check $(TARGET_FLAG)

.PHONY: lint
lint: node_modules ## Run ESLint + cargo clippy
	npm run lint
	cd src-tauri && cargo clippy $(TARGET_FLAG) -- -D warnings

.PHONY: fmt
fmt: ## Format Rust code
	cd src-tauri && cargo fmt

.PHONY: fmt-check
fmt-check: ## Check Rust formatting
	cd src-tauri && cargo fmt --check

# ──────────────────────────────────────────────
#  Build
# ──────────────────────────────────────────────

.PHONY: build
build: node_modules ## Build release binary (no bundling)
	npm run tauri build -- --no-bundle $(TARGET_FLAG)

.PHONY: build-debug
build-debug: node_modules ## Build debug binary
	npm run tauri build -- --debug --no-bundle $(TARGET_FLAG)

.PHONY: bundle
bundle: node_modules ## Build release binary + platform bundles (deb, rpm, dmg, msi…)
	npm run tauri build -- $(TARGET_FLAG)

.PHONY: bundle-deb
bundle-deb: node_modules ## Build .deb package
	npm run tauri build -- --bundles deb $(TARGET_FLAG)

.PHONY: bundle-rpm
bundle-rpm: node_modules ## Build .rpm package
	npm run tauri build -- --bundles rpm $(TARGET_FLAG)

.PHONY: bundle-appimage
bundle-appimage: node_modules ## Build AppImage
	npm run tauri build -- --bundles appimage $(TARGET_FLAG)

.PHONY: bundle-macos
bundle-macos: node_modules ## Build macOS dmg + app — run on macOS
	npm run tauri build -- --bundles dmg $(TARGET_FLAG)

.PHONY: bundle-macos-universal
bundle-macos-universal: node_modules ## Build universal macOS binary (x86_64 + aarch64)
	npm run tauri build -- --target universal-apple-darwin

.PHONY: bundle-windows
bundle-windows: node_modules ## Build Windows msi + nsis — run on Windows
	npm run tauri build -- --bundles msi,nsis $(TARGET_FLAG)

# ──────────────────────────────────────────────
#  Install / Uninstall (Linux)
# ──────────────────────────────────────────────

define DESKTOP_ENTRY
[Desktop Entry]
Name=Tusk
Comment=PostgreSQL Database Management GUI
Exec=$(BINDIR)/$(APP_NAME)
Icon=$(APP_NAME)
Terminal=false
Type=Application
Categories=Development;Database;
Keywords=postgresql;database;sql;
endef
export DESKTOP_ENTRY

.PHONY: install
install: build ## Build release and install to system (PREFIX=/usr/local)
	@mkdir -p $(DESTDIR)$(BINDIR)
	install -m755 $(TARGET_DIR)/$(APP_NAME) $(DESTDIR)$(BINDIR)/$(APP_NAME)
	@mkdir -p $(DESTDIR)$(DATADIR)/applications
	@echo "$$DESKTOP_ENTRY" > $(DESTDIR)$(DATADIR)/applications/$(APP_NAME).desktop
	@for size in 32x32 128x128; do \
		if [ -f src-tauri/icons/$$size.png ]; then \
			mkdir -p $(DESTDIR)$(DATADIR)/icons/hicolor/$$size/apps; \
			install -m644 src-tauri/icons/$$size.png \
				$(DESTDIR)$(DATADIR)/icons/hicolor/$$size/apps/$(APP_NAME).png; \
		fi; \
	done
	@echo "Installed $(APP_NAME) $(VERSION) to $(DESTDIR)$(BINDIR)/$(APP_NAME)"

.PHONY: uninstall
uninstall: ## Remove installed files
	rm -f  $(DESTDIR)$(BINDIR)/$(APP_NAME)
	rm -f  $(DESTDIR)$(DATADIR)/applications/$(APP_NAME).desktop
	rm -f  $(DESTDIR)$(DATADIR)/icons/hicolor/32x32/apps/$(APP_NAME).png
	rm -f  $(DESTDIR)$(DATADIR)/icons/hicolor/128x128/apps/$(APP_NAME).png
	@echo "Uninstalled $(APP_NAME)"

# ──────────────────────────────────────────────
#  Utilities
# ──────────────────────────────────────────────

.PHONY: clean
clean: ## Remove build artifacts
	rm -rf dist
	cd src-tauri && cargo clean

.PHONY: clean-frontend
clean-frontend: ## Remove only frontend build output
	rm -rf dist node_modules/.vite

.PHONY: deps
deps: node_modules ## Install all dependencies
	@echo "Dependencies ready"

node_modules: package.json
	npm install
	@touch node_modules

.PHONY: version
version: ## Show app version
	@echo "$(APP_NAME) $(VERSION)"

.PHONY: help
help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*##' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*## "}; {printf "  \033[36m%-22s\033[0m %s\n", $$1, $$2}'

.DEFAULT_GOAL := help
