.PHONY: build test lint fmt clean proto run-server run-client coverage help

all: fmt lint test build

build:
	@echo "Building server and client..."
	go build -o bin/openspeak-server ./cmd/openspeak-server
	go build -o bin/openspeak-client ./cmd/openspeak-client

test:
	@echo "Running tests..."
	go test -v -race -coverprofile=coverage.out ./...
	go tool cover -func=coverage.out

lint:
	@echo "Running linters..."
	go fmt ./...
	go vet ./...
	@command -v golangci-lint >/dev/null 2>&1 && golangci-lint run ./... || echo "golangci-lint not installed, skipping"

fmt:
	@echo "Formatting code..."
	go fmt ./...
	@command -v goimports >/dev/null 2>&1 && goimports -w ./... || echo "goimports not installed"

clean:
	@echo "Cleaning..."
	rm -rf bin/
	rm -f coverage.out
	rm -f coverage.html

proto:
	@echo "Generating protobuf code..."
	@command -v protoc >/dev/null 2>&1 || (echo "protoc not installed"; exit 1)
	protoc --go_out=. --go-grpc_out=. proto/*.proto

run-server:
	go run ./cmd/openspeak-server

run-client:
	go run ./cmd/openspeak-client

coverage:
	@echo "Generating coverage report..."
	go test -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html
	@echo "Report saved to coverage.html"

coverage-watch:
	watch -n 2 'make coverage'

bench:
	@echo "Running benchmarks..."
	go test -bench=. -benchmem ./...

help:
	@echo "Available targets:"
	@grep -E '^[a-zA-Z_-]+:' Makefile | sed 's/:.*//g' | sort
