IMAGE ?= go-learning:latest
ENGINE ?= podman
GO_CACHE ?= go-learning-mod-cache
PKG ?=

RUN_CONTAINER = $(ENGINE) run --rm \
	-v "$(CURDIR):/workspace:Z" \
	-v "$(GO_CACHE):/go/pkg/mod" \
	-w /workspace \
	$(IMAGE)

.PHONY: help image check lint test test-project test-all fmt fmt-check shell

help:
	@echo "Dostępne komendy:"
	@echo "  make image                                  - Zbuduj obraz kontenera Go (jednorazowo)"
	@echo "  make check                                  - Sprawdź kompilację i go vet"
	@echo "  make lint                                   - Uruchom staticcheck"
	@echo "  make test PKG=./exercises/01-core/01-basics - Uruchom testy wybranego rozdziału (-race)"
	@echo "  make test                                   - Uruchom testy 01-core + 02-engineering-2026"
	@echo "  make test-project                           - Uruchom testy mini-projektu incident-tracker"
	@echo "  make test-all                               - Uruchom testy całego repozytorium"
	@echo "  make fmt                                    - Sformatuj kod (gofmt)"
	@echo "  make fmt-check                              - Sprawdź formatowanie kodu"
	@echo "  make shell                                  - Otwórz powłokę bash wewnątrz kontenera"

image:
	$(ENGINE) build -t $(IMAGE) -f Containerfile .

check:
	$(RUN_CONTAINER) go vet ./...

lint:
	$(RUN_CONTAINER) staticcheck ./...

test:
ifdef PKG
	$(RUN_CONTAINER) go test -race -v $(PKG)
else
	$(RUN_CONTAINER) go test -race -v ./exercises/01-core/... ./exercises/02-engineering-2026/...
endif

test-project:
	$(RUN_CONTAINER) go test -race -v ./projects/incident-tracker/...

test-all:
	$(RUN_CONTAINER) go test -race -v ./...

fmt:
	$(RUN_CONTAINER) gofmt -w exercises projects

fmt-check:
	$(RUN_CONTAINER) sh -c 'out=$$(gofmt -l exercises projects); test -z "$$out" || (echo "Unformatted files:"; echo "$$out"; exit 1)'

shell:
	$(ENGINE) run --rm -it \
		-v "$(CURDIR):/workspace:Z" \
		-v "$(GO_CACHE):/go/pkg/mod" \
		-w /workspace \
		$(IMAGE) bash
