.PHONY: dev build clean install frontend

# Get version from git tag
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS := -X 'daily-timer/internal/version.Version=$(VERSION)' -X 'daily-timer/internal/version.GitCommit=$(GIT_COMMIT)' -X 'daily-timer/internal/version.BuildTime=$(BUILD_TIME)'

# Development (fixed ports: Vite 5173, Wails DevServer 34115)
dev:
	wails dev -devserver localhost:34115

# Build for macOS
build: lint
	wails build -clean -ldflags "$(LDFLAGS)"
	@xattr -cr "build/bin/Daily Timer.app" 2>/dev/null || true

# Build for macOS (universal binary)
build-universal: lint
	wails build -clean -platform darwin/universal -ldflags "$(LDFLAGS)"
	@xattr -cr "build/bin/Daily Timer.app" 2>/dev/null || true

# Install frontend dependencies
frontend:
	cd frontend && npm install

# Generate Go bindings
generate:
	wails generate module

# Clean build artifacts
clean:
	rm -rf build/bin
	rm -rf frontend/dist
	rm -rf frontend/node_modules

# Run tests
test:
	go test ./...

# Format code
fmt:
	go fmt ./...
	cd frontend && npm run format 2>/dev/null || true

# Lint
lint:
	golangci-lint run ./...

# Install dependencies
deps:
	go mod download
	go mod tidy
	cd frontend && npm install

# Initialize project (first time setup)
init: deps frontend
	@echo "Project initialized. Run 'make dev' to start development."

# Release - build and package
release: lint
	@echo "Building release $(VERSION)..."
	wails build -clean -ldflags "$(LDFLAGS)"
	@xattr -cr "build/bin/Daily Timer.app" 2>/dev/null || true
	@rm -rf dist && mkdir -p dist
	cd build/bin && zip -r "../../dist/Daily-Timer-$(VERSION)-macos-arm64.zip" "Daily Timer.app"
	@shasum -a 256 "build/bin/Daily Timer.app/Contents/MacOS/daily-timer" | awk '{print $$1}' > "dist/Daily-Timer-$(VERSION)-macos-arm64.sha256"
	@echo "Release package: dist/Daily-Timer-$(VERSION)-macos-arm64.zip"
	@echo "Checksum: $$(cat dist/Daily-Timer-$(VERSION)-macos-arm64.sha256)"
	@ls -lh dist/*

# Release for both architectures
release-all: lint
	@echo "Building release $(VERSION) for all platforms..."
	@mkdir -p dist
	GOOS=darwin GOARCH=arm64 wails build -clean -o daily-timer-arm64
	cd build/bin && zip -r "../../dist/Daily-Timer-$(VERSION)-macos-arm64.zip" "Daily Timer.app"
	GOOS=darwin GOARCH=amd64 wails build -clean -o daily-timer-amd64
	cd build/bin && zip -r "../../dist/Daily-Timer-$(VERSION)-macos-amd64.zip" "Daily Timer.app"
	@echo "Release packages:"
	@ls -lh dist/*.zip

# Upload release to Gitea (requires GITEA_TOKEN env var)
# Depends on 'release' to ensure dist/ files are up-to-date
release-upload: release
	@if [ -z "$(GITEA_TOKEN)" ]; then echo "Error: GITEA_TOKEN not set"; exit 1; fi
	@echo "Creating release $(VERSION) on Gitea..."
	@RELEASE_ID=$$(curl -s -X POST \
		-H "Authorization: token $(GITEA_TOKEN)" \
		-H "Content-Type: application/json" \
		"https://git.movida.biz/api/v1/repos/bell/daily-timer/releases" \
		-d '{"tag_name": "$(VERSION)", "name": "$(VERSION)", "body": "Release $(VERSION)"}' \
		| jq -r '.id'); \
	echo "Created release ID: $$RELEASE_ID"; \
	for file in dist/*; do \
		filename=$$(basename "$$file"); \
		echo "Uploading $$filename..."; \
		curl -s -X POST \
			-H "Authorization: token $(GITEA_TOKEN)" \
			-F "attachment=@$$file" \
			"https://git.movida.biz/api/v1/repos/bell/daily-timer/releases/$$RELEASE_ID/assets?name=$$filename"; \
	done
	@echo "Done!"

# Full release cycle: build + upload (release-upload already depends on release)
release-publish: release-upload

# Help
help:
	@echo "Daily Timer - Makefile Commands"
	@echo ""
	@echo "Development:"
	@echo "  make dev          - Start development server with hot reload"
	@echo "  make build        - Build production binary for macOS"
	@echo "  make build-universal - Build universal binary (Intel + Apple Silicon)"
	@echo ""
	@echo "Release:"
	@echo "  make release         - Build and package for current arch"
	@echo "  make release-all     - Build for all macOS architectures"
	@echo "  make release-upload  - Upload dist/*.zip to Gitea (needs GITEA_TOKEN)"
	@echo "  make release-publish - Full cycle: build + upload"
	@echo ""
	@echo "Setup:"
	@echo "  make init         - Initialize project (install all dependencies)"
	@echo "  make deps         - Install Go and frontend dependencies"
	@echo "  make frontend     - Install frontend dependencies only"
	@echo ""
	@echo "Maintenance:"
	@echo "  make clean        - Remove build artifacts"
	@echo "  make fmt          - Format Go and frontend code"
	@echo "  make lint         - Run linters"
	@echo "  make test         - Run tests"
	@echo ""
	@echo "Misc:"
	@echo "  make generate     - Regenerate Wails bindings"
	@echo "  make help         - Show this help"
