.PHONY: install lint fmt typecheck test cov up down logs ps run demo-repo demo-up demo-down local-proxy slides

install:
	uv sync

lint:
	uv run ruff check .
	uv run ruff format --check .
	uv run mypy

fmt:
	uv run ruff check --fix .
	uv run ruff format .

test:
	uv run pytest -q

cov:
	uv run pytest -q --cov --cov-report=term

up:
	@test -f .env || { cp .env.example .env; echo "created .env from .env.example — fill in the secrets"; }
	docker compose up -d --build

down:
	docker compose down

logs:
	docker compose logs -f --tail=100 bot

ps:
	docker compose ps

# Run the bot on this machine without Docker: SQLite (./tgit.db) and the settings from .env
run:
	uv run python -m app

# A local uploads repo seeded from the template — try the bot without SourceCraft:
#   make demo-repo  →  put the printed GIT_UPLOADS_URL into .env  →  make run
DEMO_REPO := $(CURDIR)/data/demo-uploads.git
demo-repo:
	@test ! -e $(DEMO_REPO) || { echo "$(DEMO_REPO) already exists"; exit 1; }
	@rm -rf data/demo-seed && mkdir -p data && cp -r infra/uploads-template data/demo-seed
	@cd data/demo-seed && git init -q -b main && git add -A \
		&& git -c user.name=tgit-bot -c user.email=tgit-bot@users.noreply.tgit commit -qm "init: uploads template"
	@git clone -q --bare data/demo-seed $(DEMO_REPO) && rm -rf data/demo-seed
	@echo "GIT_UPLOADS_URL=$(DEMO_REPO)"

# Demo services (examples/) next to the bot: they show up in «Статус», «Логи» and raise alerts
DEMO := docker compose -f docker-compose.yml -f examples/docker-compose.yml
demo-up:
	$(DEMO) up -d demo-hello demo-crash demo-cpu-hog demo-unhealthy

demo-down:
	$(DEMO) rm -sf demo-hello demo-crash demo-cpu-hog demo-unhealthy

# Docker API for `make run` (bot outside Docker): the same read-only proxy, on localhost only.
# Then DOCKER_URL=http://127.0.0.1:23750 in .env — «Статус», «Логи» and alerts see this machine's containers.
local-proxy:
	docker run -d --rm --name tgit-local-proxy -e CONTAINERS=1 \
		-v /var/run/docker.sock:/var/run/docker.sock:ro -p 127.0.0.1:23750:2375 tecnativa/docker-socket-proxy:0.3.0

# docs/presentation.pdf from docs/presentation/slides.html. Any Chromium will do: CHROME=chromium make slides
CHROME ?= chromium
slides:
	$(CHROME) --headless --no-sandbox --disable-gpu --no-pdf-header-footer \
		--print-to-pdf=$(CURDIR)/docs/presentation.pdf file://$(CURDIR)/docs/presentation/slides.html
