# Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE
#
# Single entry point for the project's commands. `make help` lists them.
# Building and packaging delegate to the Wails Taskfile through the wails3 CLI.

GOLANGCI_LINT := go tool -modfile=tools/go.mod golangci-lint
PNPM := pnpm --dir frontend

.PHONY: help install bindings frontend dev build package package-linux package-windows package-macos release-build release-tag install-test-config \
	check test test-go test-frontend test-scripts test-real \
	lint lint-go lint-frontend lint-headers bindings-check

help: ## List the available targets
	@grep -E '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | awk -F':.*## ' '{printf "  %-16s %s\n", $$1, $$2}'

# ---------------------------------------------------------------- build

install: ## Install the frontend dependencies (pnpm, both macOS and Linux binaries)
	@$(PNPM) install

bindings: ## Regenerate the TypeScript bindings of the Go services and events
	@wails3 generate bindings -clean=true -ts -i

frontend: install ## Build the frontend into frontend/dist (embedded in the binary)
	@$(PNPM) run build

# go build / go test need frontend/dist to embed: build it once if missing.
frontend/dist/index.html:
	@$(MAKE) --no-print-directory frontend

dev: ## Run the application with hot reload
	@wails3 dev

build: ## Build the application into bin/
	@wails3 build

package: ## Package the application (macOS: bin/sidekicks.app)
	@wails3 package

package-linux: ## Release packages for Linux (deb, rpm, AppImage, tar.gz) into dist/
	@scripts/package.sh linux

package-windows: ## Release packages for Windows amd64 (installer, zip) into dist/, cross-built
	@scripts/package.sh windows

package-macos: ## Release packages for macOS (universal dmg, zip) into dist/; on macOS only
	@scripts/package.sh macos

release-build: ## On the Mac: every release package (macOS, Windows, Linux in Docker) and SHA256SUMS into dist/
	@scripts/build-release.sh

release-tag: ## Tag v<version> on main and push it: the Release workflow creates the release
	@scripts/tag-release.sh

install-test-config: ## Write the test agents configuration at the default location (absolute paths)
	@scripts/install-test-config.sh

# ---------------------------------------------------------------- tests

check: lint test ## Everything the CI runs: lint, then all the tests

test: test-go test-frontend test-scripts ## Run all the tests (no real agent, no network)

# A test that hangs fails after 5 minutes with its name and the goroutines' stacks.
test-go: frontend/dist/index.html ## Go unit tests
	@go test -timeout 5m ./...

test-frontend: ## Frontend unit tests (vitest)
	@$(PNPM) test

test-scripts: ## Tests of the shell tooling
	@scripts/agents_test.sh
	@scripts/check-license-headers_test.sh
	@scripts/package_test.sh
	@scripts/tag-release_test.sh

test-real: ## Tests against the real mm agents (needs mm and llmman; 2 agents at most)
	@go test -tags realagents -count=1 ./...

# ---------------------------------------------------------------- lint

lint: lint-go lint-frontend lint-headers ## Run every linter

lint-go: frontend/dist/index.html ## golangci-lint (version pinned in tools/go.mod)
	@$(GOLANGCI_LINT) run ./...

lint-frontend: ## svelte-check (types and Svelte warnings)
	@$(PNPM) run check

lint-headers: ## Check the licence header of every source file
	@scripts/check-license-headers.sh

bindings-check: bindings ## Fail when the committed bindings are not up to date
	@git diff --exit-code -- frontend/bindings
