UNAME_S := $(shell uname -s)

migrations_root := /tmp/payments-dev-migrations-root
arcadia_root := $(shell ya dump root)
params := $(wordlist 2,100,$(MAKECMDGOALS))
no_db_recipe := DB_USE_ARCADIA_RECIPE=False
source_root := Y_PYTHON_SOURCE_ROOT=$(arcadia_root)
entry_point := Y_PYTHON_ENTRY_POINT=":main"
pytest_args := -m pytest -p no:warnings -sv


# Code style
check-isort:
	isort --check-only --diff -r payments/

check-flake8:
	flake8 payments/

check-style: check-flake8 check-isort

ensure-isort-version-ge-5:
	isort --version | python3 -c "import itertools; import sys; import re; RE = re.compile(r'(?P<version>(\d+.?)+)', re.IGNORECASE & re.DOTALL); inp = sys.stdin.read().strip('.'); version = RE.search(inp).group('version'); five, current = list(zip(*(itertools.zip_longest([5], map(int, version.split('.')), fillvalue=0)))); assert current >= five, 'isort version %s < 5.0.0' % version; print('OK:', version)"

sort-imports: ensure-isort-version-ge-5
	isort payments/

mypy:
	MYPYPATH=$(arcadia_root) mypy -p mail.payments --show-traceback --disallow-incomplete-defs --warn-unused-ignores

cc: sort-imports check-style mypy


# Tests
tc:  # stands for "test conflicts"
	$(source_root) ./python/python -m pytest --color=yes -p no:warnings --collect-only --detect-conflicts=yes $(if $(params),$(params),payments/tests)

test:
	ya make $(ARGS) -tt --test-stdout

test-unit:
	ya make $(ARGS) -tt --test-stdout $(params) payments/tests/unit

test-functional:
	ya make $(ARGS) -tt --test-stdout $(params) payments/tests/functional

dev-unit:
	$(no_db_recipe) $(source_root) $(entry_point) ./python/python $(pytest_args) $(if $(params),$(params),payments/tests/unit)

dev-functional:
	$(no_db_recipe) $(source_root) $(entry_point) ./python/python $(pytest_args) $(if $(params),$(params),payments/tests/functional)


# Development
deps:
	ya make --checkout payments && ya make --checkout payments/tests

build_bin:
	ya make bin

build_docker:
	ya package --docker --docker-repository mail/payments package/package.json

run_bin:
	$(source_root) ./bin/bin $(params)

l_run_bin: build_bin
	./run_local.sh ./bin/bin $(params)

runserver:
	$(source_root) ./bin/bin runserver

l_runserver:
	./run_local.sh make runserver

wl_runserver:
	./run_local.sh -w make runserver

runadminserver:
	$(source_root) ./bin/bin runadminserver

l_runadminserver:
	./run_local.sh make runadminserver

wl_runadminserver:
	./run_local.sh -w make runadminserver

runsdkserver:
	$(source_root) ./bin/bin runsdkserver

l_runsdkserver:
	./run_local.sh make runsdkserver

wl_runsdkserver:
	./run_local.sh -w make runsdkserver

runworkers:
	$(source_root) ./bin/bin runworkers

l_runworkers:
	./run_local.sh make runworkers

wl_runworkers:
	./run_local.sh -w make runworkers

shell:
	$(source_root) ./bin/bin shell

l_shell:
	./run_local.sh $(params) make shell

tunnel:
	ssh -D 4444 dev -N

# Database
start-db:
ifeq ($(UNAME_S),Linux)
	# докер в убунте не может делать bind volume на директорию аркадии, смонтированной через arc
	rm -rf $(migrations_root) && mkdir -p $(migrations_root) && cp -r ./postgre $(migrations_root)
	PAYMENTS_MIGRATIONS_ROOT=$(migrations_root) docker-compose up -d paymentsdb_migrated
else
	docker-compose up -d paymentsdb_migrated
endif

restart_db:
	docker-compose rm --force --stop -v paymentsdb paymentsdb_migrate paymentsdb_migrated
	make start_db

catdblog:
	docker-compose exec paymentsdb cat "/var/lib/postgresql/data/log/dblog"

psql:
	docker-compose exec paymentsdb psql -U payments -d paymentsdb

pgcli:
	PGPASSWORD="P@ssw0rd" pgcli -h localhost -p 5252 -U payments -d paymentsdb

%:
	@:
