## @build

DOCKER_BUILDX_CMD ?= docker buildx
IMAGE_BUILD_CMD ?= $(DOCKER_BUILDX_CMD) build
IMAGE_BUILD_EXTRA_OPTS ?=
IMAGE_REGISTRY ?= inftyai
IMAGE_NAME ?= alphatrion
IMAGE_REPO := $(IMAGE_REGISTRY)/$(IMAGE_NAME)
GIT_TAG ?= $(shell git describe --tags --dirty --always)
IMG ?= $(IMAGE_REPO):$(GIT_TAG)
PLATFORMS ?= linux/arm64,linux/amd64

UV := uv
RUFF := .venv/bin/ruff
LINT_IMPORTS := .venv/bin/lint-imports
PYTEST := .venv/bin/pytest
ALEMBIC := .venv/bin/alembic
PYTHON := .venv/bin/python

FRAMEWORK := alphatrion/framework

.PHONY: build
build: lint build-dashboard
	$(UV) build

.PHONY: publish
publish: build
	$(UV) publish --token $(INFTYAI_PYPI_TOKEN)

.PHONY: up
up:
	docker compose -f $(FRAMEWORK)/docker-compose.yaml up -d

.PHONY: migrate-clickhouse
migrate-clickhouse:
	$(PYTHON) -m alphatrion.framework.migrations.clickhouse.cli migrate

.PHONY: migrate
migrate:
	$(ALEMBIC) -c $(FRAMEWORK)/alembic.ini upgrade head

.PHONY: migrate-all
migrate-all: migrate migrate-clickhouse

.PHONY: down
down:
	docker compose -f $(FRAMEWORK)/docker-compose.yaml down

.PHONY: lint
lint:
	$(RUFF) check .
	$(LINT_IMPORTS)

.PHONY: format
format:
	$(RUFF) format .
	$(RUFF) check --fix .

.PHONY: test
test: lint
	$(PYTEST) $(FRAMEWORK)/tests/unit alphatrion/swarm/tests --timeout=15

.PHONY: test-integration
test-integration: lint
	@bash -c '\
	set -e; \
	set -a; \
	source $(FRAMEWORK)/.env.integration-test; \
	set +a; \
	docker-compose -f $(FRAMEWORK)/docker-compose.test.yaml up -d; \
	trap "docker-compose -f $(FRAMEWORK)/docker-compose.test.yaml down" EXIT; \
	echo "Waiting for PostgreSQL..."; \
	until docker exec postgres-test pg_isready -U alphatr1on; do sleep 1; done; \
	echo "Waiting for ClickHouse..."; \
	until docker exec clickhouse-test clickhouse-client --query "SELECT 1"; do sleep 1; done; \
	echo "Waiting for Prometheus Push Gateway..."; \
	until curl -sf http://localhost:29091/-/ready > /dev/null 2>&1; do sleep 1; done; \
	echo "Waiting for Ollama..."; \
	until curl -sf http://localhost:21434/api/tags | grep "smollm:135m" > /dev/null; do sleep 1; done; \
	echo "Running migrations..."; \
	make migrate-all; \
	echo "Running integration tests..."; \
	$(PYTEST) $(FRAMEWORK)/tests/integration --timeout=30; \
	'
.PHONY: test-all
test-all: test test-integration

.PHONY: seed
seed:
	$(PYTHON) $(FRAMEWORK)/hack/seed.py seed

.PHONY: seed-cleanup
seed-cleanup:
	$(PYTHON) $(FRAMEWORK)/hack/seed.py cleanup

.PHONY: build-dashboard
build-dashboard:
	cd $(FRAMEWORK)/dashboard && npm install && npm run build

.PHONY: image-build
image-build:
	$(IMAGE_BUILD_CMD) -t $(IMG) \
		--platform=$(PLATFORMS) \
		$(IMAGE_BUILD_EXTRA_OPTS) -f $(FRAMEWORK)/Dockerfile ./
image-load: IMAGE_BUILD_EXTRA_OPTS=--load
image-load: image-build
image-push: IMAGE_BUILD_EXTRA_OPTS=--push
image-push: image-build

.PHONY: image-build-dashboard
image-build-dashboard:
	$(IMAGE_BUILD_CMD) -t $(IMG) \
		--platform=$(PLATFORMS) \
		$(IMAGE_BUILD_EXTRA_OPTS) ./$(FRAMEWORK)/dashboard
image-load-dashboard: IMAGE_BUILD_EXTRA_OPTS=--load
image-load-dashboard: image-build-dashboard
image-push-dashboard: IMAGE_BUILD_EXTRA_OPTS=--push
image-push-dashboard: image-build-dashboard
