APP := ai-operator
BIN := bin/$(APP)
ENV ?= /etc/ai-operator/ai-operator.env
VERSION ?= 0.1.0-tz01
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
BUILD_TIME ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS := -X ai-operator/internal/app.Version=$(VERSION) -X ai-operator/internal/app.GitCommit=$(GIT_COMMIT) -X ai-operator/internal/app.BuildTime=$(BUILD_TIME)

.PHONY: fmt test build doctor run-dry ari-observe ari-call-control media-self-test ari-media-test openai-config-check openai-smoke-test openai-tool-smoke-test fake-provider-self-test agent-self-test handoff-self-test fallback-self-test handoff-check audit-health audit-self-test redaction-self-test audit-check audit-prune-dry-run dialogue-self-test dialogue-check language-self-test language-check region-self-test region-check clean

fmt:
	go fmt ./...

test:
	go test ./...

build:
	mkdir -p bin
	go build -ldflags "$(LDFLAGS)" -o $(BIN) ./cmd/ai-operator

doctor: build
	./$(BIN) doctor --env $(ENV)

run-dry: build
	./$(BIN) run --env $(ENV) --dry-run

ari-observe: build
	./$(BIN) run --env $(ENV) --ari-events --observe-only

ari-call-control: build
	./$(BIN) run --env $(ENV) --ari-events --call-control

media-self-test: build
	./$(BIN) media-self-test --env $(ENV) --duration 3s

ari-media-test: build
	./$(BIN) run --env $(ENV) --ari-events --call-control --media --media-test-mode stats --test-call-hangup-after 10s

openai-config-check: build
	./$(BIN) doctor --env $(ENV) --check-openai-config

openai-smoke-test: build
	./$(BIN) openai-smoke-test --env $(ENV)

openai-tool-smoke-test: build
	./$(BIN) openai-tool-smoke-test --env $(ENV)

fake-provider-self-test: build
	./$(BIN) voice-provider-self-test --env $(ENV) --provider fake

agent-self-test: build
	./$(BIN) agent-self-test --env $(ENV)

handoff-self-test: build
	./$(BIN) handoff-self-test --env $(ENV)

fallback-self-test: build
	./$(BIN) fallback-self-test --env $(ENV)

handoff-check: build
	./$(BIN) doctor --env $(ENV) --check-handoff

clean:
	rm -rf bin dist tmp coverage.out

dialogue-self-test: build
	./$(BIN) dialogue-self-test --env $(ENV)

dialogue-check: build
	./$(BIN) doctor --env $(ENV) --check-dialogue

language-self-test: build
	./$(BIN) language-self-test --env $(ENV)

language-check: build
	./$(BIN) doctor --env $(ENV) --check-language

region-self-test: build
	./$(BIN) region-self-test --env $(ENV)

region-check: build
	./$(BIN) doctor --env $(ENV) --check-region

postgres-up:
	docker compose --env-file /etc/ai-operator/postgres.env -f /opt/ai-operator/deploy/docker-compose.postgres.yml up -d

postgres-down:
	docker compose --env-file /etc/ai-operator/postgres.env -f /opt/ai-operator/deploy/docker-compose.postgres.yml down

postgres-logs:
	docker logs -f ai-operator-postgres

kb-migrate: build
	./$(BIN) kb-migrate --env $(ENV)

kb-health: build
	./$(BIN) kb-health --env $(ENV)

kb-ingest-kga: build
	./$(BIN) kb-ingest --env $(ENV) --path /opt/ai-operator/knowledge/import/jsonl --format jsonl --embedding-provider fake

kb-search-sample: build
	./$(BIN) kb-search --env $(ENV) --query "Сколько стоит первичное подключение газа?" --language ru --region-code almaty_city --limit 5

kb-self-test: build
	./$(BIN) kb-self-test --env $(ENV)

embedding-smoke-test: build
	./$(BIN) embedding-smoke-test --env $(ENV)


audit-health: build
	./$(BIN) audit-health --env $(ENV)

audit-self-test: build
	./$(BIN) audit-self-test --env $(ENV)

redaction-self-test: build
	./$(BIN) redaction-self-test --env $(ENV)

audit-check: build
	./$(BIN) doctor --env $(ENV) --check-audit

audit-prune-dry-run: build
	./$(BIN) audit-prune --env $(ENV) --dry-run
