SHELL = /bin/bash
### https://makefiletutorial.com/

.env:
	@cp .env_dist .env
	@echo ">> created .env from .env_dist — fill in DOCKER_PASS before docker-push"

include .env
export

.DEFAULT_GOAL := help

# Own images go to the shared Harbor project `library` (public); `make harbor-pub` pushes them.
HARBOR_REGISTRY ?= harbor.xakki.ru
HARBOR_PROJECT  ?= library

HOST_NAME := $(shell hostname)
HOST_IP  := $(shell hostname -I 2>/dev/null | awk '{print $$1}' || echo unknown)

DOCKER_UID := $(shell id -u)
DOCKER_GID := $(shell id -g)
# Run as the host uid/gid so anything written to the bind-mounted /app (dist/,
# .venv/, lock files, tool caches) lands host-owned, not root-owned. HOME=/tmp
# gives that (arbitrary, /etc/passwd-less) uid a writable home so pip/uv/
# poetry/pdm/pipenv can resolve a cache/config dir; since system site-packages
# is root-owned, `pip install <tool>` falls back to a --user install under
# $HOME/.local, so its bin dir must be on PATH too.
DOCKER_RUNAS := --user ${DOCKER_UID}:${DOCKER_GID} -e HOME=/tmp -e PATH=/tmp/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin

docker   := docker run --cpus=${DOCKER_CPUS} --memory=${DOCKER_MEMORY} ${DOCKER_RUNAS} -v $(PWD):/app ${DOCKER_USER}/${TAG}
dockerrm := docker run --rm --cpus=${DOCKER_CPUS} --memory=${DOCKER_MEMORY} ${DOCKER_RUNAS} -v $(PWD):/app -w /app ${DOCKER_USER}/${TAG}
uv       := $(docker) uv
uvrun    := $(uv) run --extra dev

.PHONY: help run docker-login docker-push harbor-pub docker-build bash deps-i deps-u \
	cs-fix cs-check mypy pytest test build release-publish pypi-publish pypi-verify lock test-install

##@ General

help: ## Show this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) }' $(MAKEFILE_LIST)

run: docker-build deps-u test ## Full cycle: build img, update deps, test

##@ Docker

docker-login: ## Login to registry
	docker login ${HOST} -u ${DOCKER_USER} -p ${DOCKER_PASS}

docker-push: ## Push image to registry
	docker push ${DOCKER_USER}/${TAG}

harbor-pub: docker-build ## Build and push the image to Harbor (needs docker login)
	docker tag ${DOCKER_USER}/${TAG} $(HARBOR_REGISTRY)/$(HARBOR_PROJECT)/${TAG}
	docker push $(HARBOR_REGISTRY)/$(HARBOR_PROJECT)/${TAG}

docker-build: ## Pull base img + build project img
	docker pull ${PYTHON_IMAGE}
	docker build -t ${DOCKER_USER}/${TAG} --build-arg PYTHON_IMAGE=${PYTHON_IMAGE} .

bash: ## Shell into the container
	docker run -it --cpus=${DOCKER_CPUS} --memory=${DOCKER_MEMORY} ${DOCKER_RUNAS} -v $(PWD):/app ${DOCKER_USER}/${TAG} bash

##@ Dependencies

deps-i: ## Install deps (uv sync, incl. dev extra)
	$(uv) sync --extra dev

deps-u: ## Update deps (uv lock --upgrade + sync)
	$(uv) lock --upgrade
	$(uv) sync --extra dev

##@ QA

cs-fix: ## Auto-fix style (ruff format + ruff check --fix)
	$(uvrun) ruff format .
	$(uvrun) ruff check --fix .

cs-check: ## Check style, no changes (ruff format --check + ruff check)
	$(uvrun) ruff format --check .
	$(uvrun) ruff check .

mypy: ## Static analysis (mypy --strict)
	$(uvrun) mypy

pytest: ## Run unit tests
	$(uvrun) pytest

test: cs-check mypy pytest ## Full QA gate: cs-check + mypy + pytest

##@ Build & Release

build: ## Build wheel + sdist into dist/
	$(uv) build

release-publish: ## Bump version, lock, test, commit, tag, push (VERSION=x.y.z YES=1 DRY_RUN=1)
	@bash scripts/release.sh

pypi-publish: ## Rebuild + upload dist/ to PyPI (needs UV_PUBLISH_TOKEN in .env)
	@test -n "$(UV_PUBLISH_TOKEN)" || { echo "UV_PUBLISH_TOKEN is empty (set it in .env)"; exit 1; }
	docker run --rm --cpus=${DOCKER_CPUS} --memory=${DOCKER_MEMORY} ${DOCKER_RUNAS} -e UV_PUBLISH_TOKEN -v $(PWD):/app -w /app ${DOCKER_USER}/${TAG} sh -c 'rm -rf dist && uv build && uv publish'

# Four lock files are committed on purpose (not transient build output): each
# proves, independently and at any time, that a clean install resolves under
# that manager — uv.lock (uv), poetry.lock (Poetry), pdm.lock (PDM),
# Pipfile/Pipfile.lock (Pipenv). pip has no native lock format of its own; its
# install path is instead exercised by `test-install` (plain venv + pip).
lock: ## Regenerate lock files for uv/poetry/pdm/pipenv
	$(uv) lock
	$(dockerrm) sh -c "pip install --quiet --no-cache-dir poetry && poetry lock"
	$(dockerrm) sh -c "pip install --quiet --no-cache-dir pdm && pdm lock"
	$(dockerrm) sh -c "pip install --quiet --no-cache-dir pipenv && pipenv install . && pipenv lock"

test-install: ## Verify install under uv/poetry/pdm/pipenv/pip
	$(dockerrm) sh -c "uv venv /tmp/venv-uv && uv pip install --python /tmp/venv-uv . && /tmp/venv-uv/bin/python -c 'import pylog; print(pylog.__version__)'"
	$(dockerrm) sh -c "uv venv /tmp/venv-extras && uv pip install --python /tmp/venv-extras '.[redis,sqlalchemy]' && /tmp/venv-extras/bin/python -c 'import pylog, redis, sqlalchemy; print(pylog.__version__)'"
	$(dockerrm) sh -c "pip install --quiet --no-cache-dir poetry && poetry install --no-interaction"
	$(dockerrm) sh -c "pip install --quiet --no-cache-dir pdm && pdm install"
	$(dockerrm) sh -c "pip install --quiet --no-cache-dir pipenv && pipenv install ."
	$(dockerrm) sh -c "python -m venv /tmp/venv-pip && /tmp/venv-pip/bin/pip install . && /tmp/venv-pip/bin/python -c 'import pylog; print(pylog.__version__)'"

# Runs from /tmp so the local checkout can never shadow the PyPI install.
pypi-verify: ## Install xakki-pylog from PyPI in a clean venv (PYPI_VERSION=x.y.z, default latest)
	$(dockerrm) sh -c "cd /tmp && python -m venv v && v/bin/pip install --quiet --no-cache-dir 'xakki-pylog[redis,sqlalchemy]$(if $(PYPI_VERSION),==$(PYPI_VERSION))' && v/bin/python -c 'import pylog, redis, sqlalchemy; print(pylog.__version__, pylog.__file__)' $(if $(PYPI_VERSION),&& test \"\$$(v/bin/python -c 'import pylog; print(pylog.__version__)')\" = $(PYPI_VERSION))"
