.PHONY: pip_dev lint fmt test test_unit clean unit_cov_report
.PHONY: dev trace-dev local_pdp up
.PHONY: start_db start_test_db_docker
.PHONY: up down shell ps logs
.PHONY: test_cerbos up_cerbos
.PHONY: start_redis redis-cli redis_cli
.PHONY: docker_unit_lint down_unit_lint ci_unit_lint ci_unit_lint_clean
.PHONY: ci_test_integration local_test_integration up_integration
.PHONY: qa_refresh
.PHONY: bludgeon_cached_cerbos_decisions seed_policy_metadata_cache

# Used by `docker compose` dev targets that use multiple yml files.
COMPOSE_DEV_FILES = -f docker-compose.yml -f docker-compose-dev.yml

PDPCLI_LOAD_CSV_DOCKER_CMD = ./pdpcli dynamodb load_csv

PDPCLI_BLUDGEON_DOCKER_CMD = ./pdpcli redis cache_bludgeon

PDPCLI_SEED_POLICY_METADATA_DOCKER_CMD = ./pdpcli cerbos seed_policy_metadata_cache

env pip_dev:  ## Prepare environment
	uv sync --group dev --all-extras

dev: env
	uv run python -m dev

trace-dev: env
	set -o allexport; source .env; set +o allexport; uv run ddtrace-run python -m dev

lint: env
	uv run ruff check dev.py pdp tests
	uv run ruff format dev.py pdp tests --check
	uv run mypy pdp/ tests/

fmt: env
	uv run ruff check dev.py pdp tests --fix
	uv run ruff format dev.py pdp tests

test: env
	uv run py.test tests/unit/

test_unit: env
	uv run py.test \
		--cov pdp tests/unit/ \
		--cov-report xml:coverage.xml \
		--cov-report html \
		--junitxml=pyunit.xml

unit_cov_report:
	python -m http.server --directory htmlcov

clean:
	rm -rf .venv .cache .pytest_cache .mypy_cache .ruff_cache pyunit.xml .coverage coverage.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

local_pdp:
	docker build . --target deploy -t pp/ows-pdp
	docker run -d  --env-file .env -p 9001:8080 pp/ows-pdp:latest

start_db: env
	docker compose $(COMPOSE_DEV_FILES) up -d pdp-dynamodb
	./scripts/start-db.sh

up: ## Compose docker app
	docker compose $(COMPOSE_DEV_FILES) up -d --remove-orphans

up_cerbos: ## Compose cerbos service
	docker compose $(COMPOSE_DEV_FILES) up pdp-cerbos

up_integration: start_test_db_docker  ## Compose docker app for integration testing
	docker compose $(COMPOSE_DEV_FILES) up -d --remove-orphans ows-pdp-integration

down: ## Shutdown docker app
	docker compose $(COMPOSE_DEV_FILES) down

shell: ## Spawn shell inside docker
	docker compose exec ows-pdp /bin/sh

ps: ## List all processes
	docker compose ps

logs: ## View logs
	docker compose logs -f

test_cerbos:
	docker run -i \
	-v ${PWD}/cerbos/policies:/policies \
	-v ${PWD}/cerbos/tests:/tests \
	ghcr.io/cerbos/cerbos:0.50.0 compile --tests=/tests /policies

docker_unit_lint:
	docker compose up \
		--build \
		--exit-code-from unit-lint \
		--abort-on-container-exit \
		--remove-orphans \
		unit-lint

start_test_db_docker:
	@echo "Creating test_pp_identity DDB table"
	docker compose run \
		--build \
		-e Environment=test \
		-e DYNAMODB_TABLE_IDENTITY=test_pp_identity \
		--rm \
		start-db

down_unit_lint:
	docker compose down unit-lint --remove-orphans

ci_unit_lint: ci_unit_lint_clean
	mkdir -p build && chmod a+w build
	make docker_unit_lint
	make test_cerbos

ci_unit_lint_clean: down_unit_lint
	rm -rf build

ci_test_integration: down start_test_db_docker
	docker compose up \
		--always-recreate-deps \
		--force-recreate \
		--build \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--remove-orphans \
		integration-test

local_test_integration: down start_test_db_docker
	docker compose $(COMPOSE_DEV_FILES) build
	docker compose $(COMPOSE_DEV_FILES) up \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--remove-orphans \
		integration-test

start_redis:
	docker compose $(COMPOSE_DEV_FILES) up -d pdp-redis

redis_cli redis-cli: start_redis
	docker run -it --network ows-pdp_default --rm redis:7 redis-cli -h pdp-redis

bludgeon_cached_cerbos_decisions:
	echo "-- Bludgeoning allowed_tenants --" && ${PDPCLI_BLUDGEON_DOCKER_CMD} allowed_tenants
	echo "-- Bludgeoning list_tenant_roles --" && ${PDPCLI_BLUDGEON_DOCKER_CMD} list_tenant_roles

seed_policy_metadata_cache:
	${PDPCLI_SEED_POLICY_METADATA_DOCKER_CMD}
