.PHONY: ci_test_integration ci_test_integration_clean ci_unit_lint ci_unit_lint_clean \
	clean clean_all \
	docker_login docker_test_integration docker_test_integration_down docker_unit_lint docker_unit_lint_down \
	env env_dev env_integration \
	format format_fix \
	generate generate_stubs \
	help \
	lint lint_fix \
	requirements \
	test test_integration

help:
	@echo "===================="
	@echo "python-abacus-models"
	@echo "===================="
	@echo ""
	@echo "Development targets:"
	@echo "  env                     - Install production dependencies"
	@echo "  env_dev                 - Install development dependencies"
	@echo "  env_integration         - Install integration test dependencies"
	@echo ""
	@echo "Code quality targets:"
	@echo "  format                  - Check code formatting with ruff"
	@echo "  format_fix              - Fix code formatting automatically"
	@echo "  lint                    - Run linting checks with ruff"
	@echo "  lint_fix                - Fix linting issues automatically"
	@echo ""
	@echo "Testing targets:"
	@echo "  test                    - Run unit tests with coverage"
	@echo "  test_integration        - Run integration tests"
	@echo ""
	@echo "Build targets:"
	@echo "  generate                - Generate SQLAlchemy models and stubs from database"
	@echo "  generate_stubs          - Generate .pyi type stub files only"
	@echo "  requirements            - Generate requirements.txt files"
	@echo ""
	@echo "Cleanup targets:"
	@echo "  clean                   - Remove generated files and cache"
	@echo "  clean_all               - Remove everything including virtual environment"
	@echo ""
	@echo "CI/CD targets:"
	@echo "  ci_unit_lint            - Run unit tests and linting in Docker"
	@echo "  ci_test_integration     - Run integration tests in Docker"
	@echo ""
	@echo "Docker targets:"
	@echo "  docker_login            - Login to AWS ECR"
	@echo "  docker_unit_lint        - Run linting in Docker"
	@echo "  docker_test_integration - Run integration tests in Docker"

clean:
	@echo "Cleaning up..."
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".ruff_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type f -name "*.pyc" -delete 2>/dev/null || true
	@find . -type f -name "*.pyo" -delete 2>/dev/null || true
	@rm -rf -- build dist *.egg-info .cache .coverage \
		coverage.xml pyunit.xml
	@echo "Done!"

clean_all: clean
	@echo "Removing virtual environment..."
	@rm -rf .venv
	@echo "Done!"

env:
	@uv sync --frozen

env_dev:
	@uv sync --group dev --frozen

env_integration:
	@uv sync --group integration --frozen

format: env_dev
	@echo "Checking formatting..."
	@uv run --no-sync -- ruff format --check --diff

format_fix: env_dev
	@echo "Fixing formatting..."
	@uv run --no-sync -- ruff format

generate: env_dev
	@echo "Generating models..."
	@uv run --no-sync -- python -m generator
	@echo "Generated models"
	@make generate_stubs

generate_stubs: env_dev
	@echo "Generating stub files..."
	@echo "- Core:"
	@uv run --no-sync -- stubgen -p abacus_models.core -o . --include-docstrings
	@echo "- Models:"
	@find abacus_models/mysql abacus_models/snowflake -name "generated.py" -type f 2>/dev/null | while read file; do \
		echo "    - $$file"; \
		uv run --no-sync -- stubgen "$$file" -o . --no-import 2>/dev/null || true; \
	done
	@echo "Generated stubs"
	@make format_fix
	@make lint_fix

lint: env_dev
	@echo "Running linter..."
	@uv run --no-sync -- ruff check

lint_fix: env_dev
	@echo "Fixing linting..."
	@uv run --no-sync -- ruff check --fix

requirements:
	@echo "Creating requirements.txt..."
	./scripts/generate_requirements.py
	@echo "Creating requirements-dev.txt..."
	./scripts/generate_requirements.py -g dev
	@echo "Done!"

test: env_dev
	./scripts/test.sh

test_integration:
	@uv run --no-sync -- python -m pytest \
		tests/integration \
		 --cov abacus_models \
		--cov-report xml \
		--junitxml=build/pyunit.xml

#############
### CI/CD ###
#############

ci_test_integration: ci_test_integration_clean docker_test_integration

ci_test_integration_clean: docker_test_integration_down

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

ci_unit_lint_clean: docker_unit_lint_down
	rm -rf build

##############
### Docker ###
##############

docker_login:
	aws ecr get-login-password --region us-east-1 | \
		docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com

docker_test_integration:
	docker compose up \
		--build \
		--exit-code-from test-integration \
		--abort-on-container-exit \
		--remove-orphans \
		test-integration

docker_test_integration_down: 
	docker compose down test-integration --volumes --remove-orphans

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

docker_unit_lint_down:
	docker compose down unit-lint --volumes --remove-orphans
