.PHONY: help build run test docker-build docker-up docker-down docker-logs clean fmt lint vet

help: ## Display this help screen
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

build: ## Build the application
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o bin/server ./cmd/server/main.go

run: build ## Build and run the application locally
	MONGO_URI=mongodb://localhost:27017 REDIS_ADDR=localhost:6379 PORT=8080 ./bin/server

dev: ## Run with live reload (requires air: go install github.com/cosmtrek/air@latest)
	air

test: ## Run all tests
	go test -v -race -coverprofile=coverage.out ./...

test-coverage: test ## Run tests and open coverage report
	go tool cover -html=coverage.out -o coverage.html
	open coverage.html

bench: ## Run benchmarks
	go test -bench=. -benchmem ./...

fmt: ## Format Go code
	gofmt -s -w .

vet: ## Run go vet
	go vet ./...

lint: ## Run linter (requires golangci-lint)
	golangci-lint run ./...

docker-build: ## Build Docker image
	docker build -t chatserver:latest .

docker-up: ## Start services with Docker Compose
	docker-compose up -d

docker-down: ## Stop services with Docker Compose
	docker-compose down

docker-stop: ## Stop services without removing volumes
	docker-compose stop

docker-logs: ## View Docker Compose logs
	docker-compose logs -f app

docker-clean: docker-down ## Stop services and remove volumes
	docker-compose down -v

deps: ## Download and verify dependencies
	go mod download
	go mod verify

tidy: ## Tidy dependencies
	go mod tidy

check: fmt vet test ## Run all checks (format, vet, test)

clean: ## Clean build artifacts
	rm -rf bin/
	rm -f coverage.out coverage.html
	rm -f *.prof

mongo-shell: ## Open MongoDB shell
	docker exec -it chatserver-mongo-1 mongosh

redis-cli: ## Open Redis CLI
	docker exec -it chatserver-redis-1 redis-cli

.DEFAULT_GOAL := help
