.PHONY: build run test clean docker-build docker-run docker-stop logs dev-setup lint fmt tidy docker-upgrade

# Build the application
build:
	@echo "Building SEO Optimizer..."
	go build -o bin/seo-optimizer ./cmd/optimizer

# Run the application locally
run:
	@echo "Running SEO Optimizer..."
	@set -a; [ -f .env ] && . ./.env; set +a; \
		go run ./cmd/optimizer

# Run tests
test:
	@echo "Running tests..."
	go test -v ./...

# Run tests with coverage
test-coverage:
	@echo "Running tests with coverage..."
	go test -v -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report generated: coverage.html"

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

# Docker build
docker-build:
	@echo "Building Docker image..."
	docker build -t seo-optimizer:latest .

# Docker run with docker-compose
docker-run:
	@echo "Starting with docker-compose..."
	docker-compose up -d

# Docker stop
docker-stop:
	@echo "Stopping docker-compose..."
	docker-compose down

# Docker upgrade (rebuild + recreate with fresh images)
docker-upgrade:
	@echo "Stopping docker-compose..."
	docker-compose down --remove-orphans
	@echo "Removing Docker images for seo-optimizer..."
	@docker image rm -f seo-optimizer:latest 2>/dev/null || true
	@echo "Pruning dangling images..."
	@docker image prune -f
	@echo "Rebuilding and starting with docker-compose..."
	docker-compose up -d --build --force-recreate

# View logs
logs:
	docker-compose logs -f

# Development setup
dev-setup:
	@echo "Setting up development environment..."
	@if [ ! -f .env ]; then \
		cp .env.example .env; \
		echo "Created .env file - please edit with your credentials"; \
	fi
	@if [ ! -f configs/config.yaml ]; then \
		cp configs/config.example.yaml configs/config.yaml; \
		echo "Created configs/config.yaml - please customize if needed"; \
	fi
	@echo "Development environment ready!"
	@echo "Next steps:"
	@echo "  1. Edit .env with your WordPress and LLM credentials"
	@echo "  2. Run 'make tidy' to download dependencies"
	@echo "  3. Run 'make run' to start the application"

# Lint code
lint:
	@echo "Running linter..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run; \
	else \
		echo "golangci-lint not installed. Install with:"; \
		echo "  go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
	fi

# Format code
fmt:
	@echo "Formatting code..."
	go fmt ./...

# Tidy dependencies
tidy:
	@echo "Tidying dependencies..."
	go mod tidy

# Install dependencies
deps:
	@echo "Installing dependencies..."
	go get github.com/spf13/viper
	go get github.com/gorilla/mux
	go get github.com/google/uuid
	go mod tidy

# Build for production (with optimizations)
build-prod:
	@echo "Building for production..."
	CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o bin/seo-optimizer ./cmd/optimizer

# Build the CLI client
cli:
	@echo "Building CLI..."
	go build -o bin/wp-sk-cli ./cmd/cli

# Run with DEBUG logging
run-debug:
	@echo "Running with DEBUG logging..."
	@set -a; [ -f .env ] && . ./.env; set +a; \
		LOG_LEVEL=DEBUG go run ./cmd/optimizer

# Quick test (no verbose output)
test-quick:
	@echo "Running quick tests..."
	go test ./...

# All: clean, format, lint, test, build
all: clean fmt lint test build
	@echo "Build complete!"

# Help
help:
	@echo "Available targets:"
	@echo "  build         - Build the application"
	@echo "  run           - Run the application locally"
	@echo "  run-debug     - Run with DEBUG logging"
	@echo "  test          - Run all tests"
	@echo "  test-coverage - Run tests with coverage report"
	@echo "  test-quick    - Run tests without verbose output"
	@echo "  clean         - Clean build artifacts"
	@echo "  docker-build  - Build Docker image"
	@echo "  docker-run    - Start with docker-compose"
	@echo "  docker-stop   - Stop docker-compose"
	@echo "  docker-upgrade- Rebuild, prune images, and recreate containers"
	@echo "  logs          - View docker-compose logs"
	@echo "  dev-setup     - Set up development environment"
	@echo "  deps          - Install dependencies"
	@echo "  lint          - Run linter"
	@echo "  fmt           - Format code"
	@echo "  tidy          - Tidy dependencies"
	@echo "  build-prod    - Build for production"
	@echo "  all           - Clean, format, lint, test, and build"
	@echo "  help          - Show this help message"
