.PHONY: build run test clean install help

# Build variables
BINARY_NAME=etl
BUILD_DIR=bin
GO=go
GOFLAGS=-v

help: ## Show this help message
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Available targets:'
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

install: ## Install dependencies
	$(GO) mod download
	$(GO) mod tidy

build: ## Build the binary
	@mkdir -p $(BUILD_DIR)
	$(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/etl

run: build ## Build and run the application
	./$(BUILD_DIR)/$(BINARY_NAME) pipeline --config configs/pipeline.json --verbose

run-csv: build ## Run with CSV input
	@mkdir -p output
	./$(BUILD_DIR)/$(BINARY_NAME) pipeline \
		--extract-type csv \
		--extract-file testdata/sales.csv \
		--load-type json \
		--load-file output/sales.json \
		--verbose

run-json: build ## Run with JSON input
	@mkdir -p output
	./$(BUILD_DIR)/$(BINARY_NAME) pipeline \
		--extract-type json \
		--extract-file testdata/sales.json \
		--load-type json \
		--load-file output/sales_out.json \
		--verbose

run-db: build ## Run with database output
	@mkdir -p output
	./$(BUILD_DIR)/$(BINARY_NAME) pipeline \
		--extract-type csv \
		--extract-file testdata/sales.csv \
		--load-type database \
		--load-file output/sales.db \
		--verbose

run-filter: build ## Run with filter
	@mkdir -p output
	./$(BUILD_DIR)/$(BINARY_NAME) pipeline \
		--extract-type csv \
		--extract-file testdata/sales.csv \
		--load-type json \
		--load-file output/filtered.json \
		--filter "quantity > 10" \
		--verbose

test: ## Run tests
	$(GO) test -v ./...

test-coverage: ## Run tests with coverage
	$(GO) test -v -coverprofile=coverage.out ./...
	$(GO) tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report: coverage.html"

test-race: ## Run tests with race detector
	$(GO) test -v -race ./...

bench: ## Run benchmarks
	$(GO) test -bench=. -benchmem ./...

fmt: ## Format code
	$(GO) fmt ./...

vet: ## Run go vet
	$(GO) vet ./...

lint: ## Run golangci-lint (requires golangci-lint)
	golangci-lint run

clean: ## Clean build artifacts
	rm -rf $(BUILD_DIR)
	rm -rf output
	rm -f coverage.out coverage.html

.DEFAULT_GOAL := help
