.PHONY: help install setup-dev dev-up dev-down prod-up prod-down logs test test-prod test-cov test-fast test-parallel test-failfast lint format type-check security-check pre-commit pre-push migrate createsuperuser shell clean

COMPOSE_DEV = docker compose -f docker-compose.dev.yml
COMPOSE_PROD = docker compose -f docker-compose.prod.yml --env-file .env.prod

help:
	@echo "Available commands:"
	@echo "  make install          - Install dependencies (uv sync --dev)"
	@echo "  make setup-dev        - Install git hooks (pre-commit + pre-push)"
	@echo "  make dev-up           - Start local stack (dev compose)"
	@echo "  make dev-down         - Stop local stack"
	@echo "  make prod-up          - Start prod compose (without db/redis)"
	@echo "  make prod-down        - Stop prod compose"
	@echo "  make logs             - Follow logs from dev compose"
	@echo "  make test             - Run tests (use TARGET=... to filter)"
	@echo "  make test-prod        - Run production-like tests (PostgreSQL + migrations)"
	@echo "  make pre-commit       - Run all pre-commit checks"
	@echo "  make pre-push         - Run pre-push checks (tests)"
	@echo "  make migrate          - Run Django migrations"
	@echo "  make createsuperuser  - Create Django superuser"
	@echo "  make shell            - Open Django shell"
	@echo "  make clean            - Cleanup caches and artifacts"

install:
	uv sync --dev

setup-dev: install
	./scripts/setup-precommit.sh

dev-up:
	$(COMPOSE_DEV) up -d

dev-down:
	$(COMPOSE_DEV) down

prod-up:
	$(COMPOSE_PROD) up -d

prod-down:
	$(COMPOSE_PROD) down

logs:
	$(COMPOSE_DEV) logs -f

test:
	@if [ "$(TARGET)" ]; then \
		echo "Running tests: $(TARGET)"; \
		if [ "$(TARGET)" = "user" ]; then \
			./scripts/run-tests.sh ../tests/apps/user; \
		elif [ "$(TARGET)" = "models" ] || [ "$(TARGET)" = "views" ] || [ "$(TARGET)" = "serializers" ] || [ "$(TARGET)" = "services" ]; then \
			./scripts/run-tests.sh ../tests -k "test_$(TARGET)"; \
		elif echo "$(TARGET)" | grep -q '^test_'; then \
			./scripts/run-tests.sh ../tests -k "$(TARGET)"; \
		else \
			./scripts/run-tests.sh "$(TARGET)"; \
		fi; \
	else \
		echo "Running full test suite"; \
		./scripts/run-tests.sh; \
	fi

test-prod:
	@if [ "$(TARGET)" ]; then \
		echo "Running production-like tests: $(TARGET)"; \
		./scripts/run-tests-prod.sh "$(TARGET)"; \
	else \
		echo "Running full production-like test suite"; \
		./scripts/run-tests-prod.sh; \
	fi

test-cov:
	./scripts/run-tests.sh ../tests --cov=src --cov-report=term-missing --cov-report=xml --cov-report=html

test-fast:
	./scripts/run-tests.sh ../tests -m "not slow"

test-parallel:
	./scripts/run-tests.sh ../tests -n auto || ./scripts/run-tests.sh ../tests

test-failfast:
	./scripts/run-tests.sh ../tests -x

pre-commit:
	uv run pre-commit run --all-files

pre-push:
	uv run pre-commit run --hook-stage pre-push --all-files

lint: pre-commit

format:
	uv run pre-commit run --all-files ruff-format
	uv run pre-commit run --all-files ruff

type-check: pre-commit

security-check: pre-commit

migrate:
	cd src && uv run python manage.py makemigrations
	cd src && uv run python manage.py migrate

createsuperuser:
	cd src && uv run python manage.py createsuperuser

shell:
	cd src && uv run python manage.py shell

clean:
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	rm -rf *.log htmlcov .coverage coverage.xml bandit-report.json
	rm -rf .pytest_cache .mypy_cache .ruff_cache
