# Makefile for running the Vet Clinic Chat Assistant locally with Ollama

.PHONY: run ollama-start ollama-stop ollama-pull ollama-status

# Start Ollama server (if not already running)
ollama-start:
	ollama serve &
	@echo "Ollama server started."

# Stop Ollama server
ollama-stop:
	pkill -f "ollama serve" || true
	@echo "Ollama server stopped."

# Pull a model (default: llama3)
ollama-pull:
	ollama pull qwen3:latest

# Show Ollama status
ollama-status:
	ollama list

# Database configuration (override via: make run DB_PASSWORD=secret DB_NAME=other)
DB_HOST ?= localhost
DB_PORT ?= 5432
DB_USER ?= postgres
DB_PASSWORD ?= postgres
DB_NAME ?= ledger-balance-service
DB_SSLMODE ?= disable

# Derived env export snippet for DB
db_env = PGHOST=$(DB_HOST) PGPORT=$(DB_PORT) PGUSER=$(DB_USER) PGPASSWORD=$(DB_PASSWORD) PGDATABASE=$(DB_NAME) PGSSLMODE=$(DB_SSLMODE)

# Run the Go server (assumes Ollama is running) with DB env vars
run: ollama-pull
	$(db_env) OPENAI_API_KEY=ollama OPENAI_BASE_URL=http://localhost:11434/api/chat OPENAI_MODEL=qwen3:latest go run .

# Run without pulling model (faster if already present)
run-fast:
	$(db_env) OPENAI_API_KEY=ollama OPENAI_BASE_URL=http://localhost:11434/api/chat OPENAI_MODEL=qwen3:latest go run .

# Quick psql shell (requires psql installed)
psql:
	$(db_env) psql || true

# Print the DSN that main.go will assemble
print-dsn:
	@echo postgres://$(DB_USER):******@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=$(DB_SSLMODE)

# Run tests
.PHONY: test

test:
	go test ./...
