.PHONY: dev dev-down db db-reset seed seed-dev migrate migrate-status bootstrap-admin test test-integration test-frontend test-mobile mobile-android mobile-ios check-docs agent-evals steel-thread clean raga-lakshana-checks mint-guard

COMPOSE := docker compose
# Flyway runs as the compose `migrate` service (flyway/flyway image) on the db network.
FLYWAY  := $(COMPOSE) run --rm migrate
PSQL_DB := $(COMPOSE) exec -T db psql -U postgres -v ON_ERROR_STOP=1

# Start full dev stack
dev:
	$(COMPOSE) --profile dev up --build

# Stop dev stack
dev-down:
	$(COMPOSE) --profile dev down

# Start database only
db:
	$(COMPOSE) up -d db

# Reset database: drop -> create -> Flyway migrate (applies V__ schema + R__ reference data)
db-reset:
	$(COMPOSE) up -d --wait db
	$(PSQL_DB) -d postgres \
	  -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='sangita_grantha' AND pid<>pg_backend_pid();" \
	  -c "DROP DATABASE IF EXISTS sangita_grantha;" \
	  -c "CREATE DATABASE sangita_grantha;"
	$(FLYWAY)

# Run pending migrations (Flyway: V__ versioned + R__ repeatable reference data)
migrate:
	$(FLYWAY)

# Show migration + repeatable status
migrate-status:
	$(FLYWAY) info

# Dev-only sample content (never a migration, never CI). Reference data arrives via `migrate`.
seed-dev:
	$(PSQL_DB) -d sangita_grantha < database/seed_data/02_sample_data.sql

# Back-compat alias: reference data is now applied by `make migrate`; this loads dev sample only.
seed: seed-dev

# Provision/update the admin user with an argon2id hash (TRACK-114 helper).
# Requires ADMIN_EMAIL and ADMIN_PASSWORD in the environment; connects to DB_HOST (default localhost).
bootstrap-admin:
	./gradlew :modules:backend:api:bootstrapAdmin

# Run backend tests (unit + integration; integration self-provisions Postgres via Testcontainers)
test:
	./gradlew :modules:backend:dal:test :modules:backend:api:test

# Run only the @Tag("integration") tests (DAL D1–D6 suite + api integration)
test-integration:
	./gradlew :modules:backend:dal:integrationTest :modules:backend:api:integrationTest

# Run frontend tests
test-frontend:
	cd modules/frontend/sangita-admin-web && bun run test:unit

# TRACK-138: shared mobile client/presenter tests (JVM)
test-mobile:
	./gradlew :modules:shared:mobile-data:jvmTest :modules:shared:presentation:jvmTest

# TRACK-138: Android debug APK
mobile-android:
	./gradlew :modules:mobile:androidApp:assembleDebug

# TRACK-138: iOS simulator host. Locally prefers iOS 26.5; CI uses a generic
# destination (RASIKA_IOS_DESTINATION). Pass a destination as the first argument.
# TRACK-140 journeys require an explicit device and never fall back:
#   bash tools/mobile/verify-rasika-journeys.sh android <serial>
#   bash tools/mobile/verify-rasika-journeys.sh ios <udid>
mobile-ios:
	bash tools/mobile/verify-ios.sh

# Verify every relative Markdown link resolves (docs rot silently when files move)
check-docs:
	python3 tools/check-doc-links.py

# Deterministic agent-config evals (CLAUDE.md, skills, hooks, REVIEW.md)
agent-evals:
	python3 evals/check.py

# TRACK-136 §3.2: janya⊄parent + mela-as-own-janya (requires a migrated DB)
raga-lakshana-checks:
	$(PSQL_DB) -d sangita_grantha -f database/checks/raga_lakshana.sql

# TRACK-136: no live SQL mint of ragas outside Flyway migrations
mint-guard:
	@if git grep -n -I -E 'INSERT[[:space:]]+INTO[[:space:]]+ragas[[:space:]]*\(' -- \
		'*.py' '*.kt' '*.ts' '*.sql' \
		':!database/migrations/**' \
		':!archive/**'; then \
		echo "ERROR: INSERT INTO ragas is forbidden outside database/migrations (TRACK-136)" >&2; \
		exit 1; \
	fi
	@echo "mint-guard: clean"

# Run steel thread E2E test

# Run steel thread E2E test
steel-thread:
	./gradlew :modules:backend:api:test --tests "*SteelThread*"

# Clean everything
clean:
	$(COMPOSE) down -v
