.PHONY: test test-short test-cover lint build install clean help cross release-notes

SHELL := /bin/bash

SHELL := /bin/bash

# Versioning (override on command line or via env for releases)
VERSION ?= dev-$(shell git describe --tags --always --dirty 2>/dev/null || echo unknown)
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo unknown)

MODULE = github.com/gmgauthier/gostations
LDFLAGS = -s -w \
	-X '$(MODULE)/internal/version.Version=$(VERSION)' \
	-X '$(MODULE)/internal/version.Commit=$(COMMIT)' \
	-X '$(MODULE)/internal/version.BuildDate=$(DATE)' \
	-X 'main.version=$(VERSION)'

test:
	go test ./... -v -race

test-short:
	go test -short ./... -v -race

test-cover:
	@mkdir -p build
	go test ./... -coverprofile=build/coverage.out
	go tool cover -html=build/coverage.out -o build/coverage.html
	@echo "✅ Coverage report: open build/coverage.html in your browser"

lint:
	@which golangci-lint > /dev/null || (echo "❌ golangci-lint not found. Install with:" && echo "  go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest" && exit 1)
	golangci-lint run

deps:
	go mod download

build: deps
	@mkdir -p build
	go build -trimpath -ldflags "$(LDFLAGS)" -o build/gostations .
	@echo "✅ Dev build: VERSION=$(VERSION) COMMIT=$(COMMIT) DATE=$(DATE)"
	@build/gostations -v || true

install: build
	mkdir -p ~/.local/bin
	cp build/gostations ~/.local/bin/gostations
	chmod +x ~/.local/bin/gostations
	@echo "✅ gostations installed to ~/.local/bin/gostations"

clean:
	rm -rf build/

# Cross compile (used by release workflow)
cross: deps
	@mkdir -p build
	@for plat in 'linux/amd64' 'linux/arm64' 'darwin/amd64' 'darwin/arm64' 'windows/amd64'; do \
		IFS='/' read -r OS ARCH <<< "$$plat"; \
		BIN="gostations-$$OS-$$ARCH"; \
		if [ "$$OS" = "windows" ]; then BIN="$$BIN.exe"; fi; \
		echo "Building $$BIN..."; \
		GOOS=$$OS GOARCH=$$ARCH go mod download; \
		GOOS=$$OS GOARCH=$$ARCH go build -trimpath -ldflags "$(LDFLAGS)" -o "build/$$BIN" . ; \
	done
	@echo "✅ Cross builds complete in build/"

# Helper to print release notes body (used by release process)
release-notes:
	@echo "## Installation"
	@echo ""
	@echo "Download the appropriate archive for your platform from the release assets."
	@echo ""
	@echo "### Quick install (Linux/macOS)"
	@echo ""
	@echo '```bash'
	@echo 'curl -L https://repos.gmgauthier.com/gmgauthier/gostations/releases/download/$(VERSION)/gostations-install.sh | VERSION=$(VERSION) bash'
	@echo '```'
	@echo ""
	@echo "### Quick install (Windows / PowerShell)"
	@echo ""
	@echo '```powershell'
	@echo 'irm https://repos.gmgauthier.com/gmgauthier/gostations/releases/download/$(VERSION)/gostations-install.ps1 | iex'
	@echo '```'
	@echo ""
	@echo "See README.md for full details and configuration (radiostations.ini)."

help:
	@echo "Available targets:"
	@echo "  test          Run all tests (with race)"
	@echo "  test-short    Run tests with -short (skips live integration)"
	@echo "  test-cover    Tests + HTML coverage report"
	@echo "  lint          Run golangci-lint"
	@echo "  build         Optimized dev build (uses git describe for VERSION)"
	@echo "  install       Build + install to ~/.local/bin/gostations"
	@echo "  cross         Build all release platforms into build/"
	@echo "  release-notes Print suggested Gitea release body text"
	@echo "  clean         Remove build/"
