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 check-all test test-project test-all clippy clippy-all fmt fmt-check shell clean

help:
	@echo "Dostępne komendy:"
	@echo "  make image                       - Zbuduj obraz kontenera Rust (jednorazowo)"
	@echo "  make check                       - Szybki check 01-core + 02-engineering-2026 (czyste std)"
	@echo "  make check-all                   - Pełny check całego workspace (razem z 03-ecosystems i projects)"
	@echo "  make test PKG=core01-basics      - Uruchom testy wybranego rozdziału"
	@echo "  make test                        - Uruchom testy domyślnych ścieżek (01 + 02)"
	@echo "  make test-project                - Uruchom testy mini-projektu incident-tracker"
	@echo "  make test-all                    - Uruchom testy całego workspace"
	@echo "  make clippy                      - Linter Clippy dla 01-core + 02-engineering-2026"
	@echo "  make clippy-all                  - Linter Clippy dla całego workspace"
	@echo "  make fmt                         - Sformatuj kod (rustfmt)"
	@echo "  make fmt-check                   - Sprawdź formatowanie kodu"
	@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 --tests

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

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

test-project:
	$(RUN_CONTAINER) cargo test -p incident-tracker -- --nocapture

test-all:
	$(RUN_CONTAINER) cargo test --workspace

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

clippy-all:
	$(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
