IMAGE ?= rust-learning:latest
ENGINE ?= podman
CARGO_CACHE ?= rust-learning-cargo-cache
PKG ?=

RUN_CONTAINER = $(ENGINE) run --rm \
	-v "$(CURDIR):/workspace:Z" \
	-v "$(CARGO_CACHE):/usr/local/cargo/registry" \
	-w /workspace \
	$(IMAGE)

.PHONY: help image check test clippy fmt fmt-check shell clean

help:
	@echo "Dostępne komendy:"
	@echo "  make image                  - Zbuduj obraz kontenera Rust (jednorazowo)"
	@echo "  make check                  - Sprawdź kompilację całego workspace"
	@echo "  make test                   - Uruchom testy wszystkich ćwiczeń"
	@echo "  make test PKG=ex01-basics   - Uruchom testy wybranego rozdziału"
	@echo "  make clippy                 - Uruchom linter Clippy"
	@echo "  make fmt                    - Sformatuj kod (rustfmt)"
	@echo "  make shell                  - Otwórz powłokę bash wewnątrz kontenera"
	@echo "  make clean                  - Usuń katalog target/"

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

check:
	$(RUN_CONTAINER) cargo check --workspace --tests

test:
ifdef PKG
	$(RUN_CONTAINER) cargo test -p $(PKG) -- --nocapture
else
	$(RUN_CONTAINER) cargo test --workspace
endif

clippy:
	$(RUN_CONTAINER) cargo clippy --workspace --all-targets

fmt:
	$(RUN_CONTAINER) cargo fmt --all

fmt-check:
	$(RUN_CONTAINER) cargo fmt --all -- --check

shell:
	$(ENGINE) run --rm -it \
		-v "$(CURDIR):/workspace:Z" \
		-v "$(CARGO_CACHE):/usr/local/cargo/registry" \
		-w /workspace \
		$(IMAGE) bash

clean:
	$(RUN_CONTAINER) cargo clean
