.PHONY: help test test-coverage lint fmt fmt-check build clean docker-build docker-run generate verify
.PHONY: docs-install docs-serve docs-build docs-deploy docs-clean docs-validate
.PHONY: docs-docker-build docs-docker-serve docs-docker-build-site docs-docker-clean docs-docker-compose-up docs-docker-compose-down

# Variables
GO := go
BINARY_NAME := platform
BINARY_PATH := bin/$(BINARY_NAME)
DOCKER_IMAGE := goplt:latest

# Default target
help:
	@echo "Available targets:"
	@echo ""
	@echo "Development commands:"
	@echo "  make test          - Run all tests"
	@echo "  make test-coverage - Run tests with coverage report"
	@echo "  make lint          - Run linters"
	@echo "  make fmt           - Format code"
	@echo "  make fmt-check     - Check code formatting"
	@echo "  make build         - Build platform and api-gateway binaries"
	@echo "  make clean         - Clean build artifacts"
	@echo "  make docker-build  - Build Docker image"
	@echo "  make docker-run    - Run Docker container"
	@echo "  make generate      - Run code generation"
	@echo "  make verify        - Verify code (fmt, lint, test)"
	@echo ""
	@echo "Documentation commands (require Python/pip):"
	@echo "  make docs-install    - Install MkDocs dependencies"
	@echo "  make docs-serve      - Serve documentation locally (http://127.0.0.1:8000)"
	@echo "  make docs-build      - Build static documentation site"
	@echo "  make docs-deploy     - Deploy documentation to GitHub Pages"
	@echo "  make docs-clean      - Clean build artifacts"
	@echo "  make docs-validate   - Validate MkDocs configuration"
	@echo ""
	@echo "Docker commands (no Python installation required):"
	@echo "  make docs-docker-build        - Build Docker image for MkDocs"
	@echo "  make docs-docker-serve        - Serve documentation using Docker"
	@echo "  make docs-docker-build-site   - Build static site in Docker container"
	@echo "  make docs-docker-clean        - Clean Docker images and containers"
	@echo "  make docs-docker-compose-up   - Start docs server with docker-compose"
	@echo "  make docs-docker-compose-down - Stop docs server with docker-compose"
	@echo ""
	@echo "Documentation shortcuts:"
	@echo "  make docs            - Alias for docs-serve"
	@echo "  make build-docs      - Alias for docs-build"
	@echo "  make docs-docker     - Alias for docs-docker-serve"

# Development commands
test:
	@echo "Running tests..."
	CGO_ENABLED=1 $(GO) test -v -race ./...

test-coverage:
	@echo "Running tests with coverage..."
	CGO_ENABLED=1 $(GO) test -v -race -coverprofile=coverage.out ./...
	$(GO) tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report generated: coverage.html"

lint:
	@echo "Running linters..."
	@if command -v golangci-lint > /dev/null; then \
		golangci-lint run; \
	else \
		echo "golangci-lint not found. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
		exit 1; \
	fi

fmt:
	@echo "Formatting code..."
	$(GO) fmt ./...
	@if command -v goimports > /dev/null; then \
		goimports -w .; \
	else \
		echo "goimports not found. Install with: go install golang.org/x/tools/cmd/goimports@latest"; \
	fi

fmt-check:
	@echo "Checking code formatting..."
	@if [ "$(shell gofmt -s -l . | wc -l)" -gt 0 ]; then \
		echo "The following files need formatting:"; \
		gofmt -s -d .; \
		exit 1; \
	fi
	@echo "Code is properly formatted"

build:
	@echo "Building platform and api-gateway binaries..."
	$(GO) build -v -o bin/platform ./cmd/platform
	$(GO) build -v -o bin/api-gateway ./cmd/api-gateway
	@echo "Build complete: bin/platform, bin/api-gateway"

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

docker-build:
	@echo "Building Docker image..."
	docker build -t $(DOCKER_IMAGE) .
	@echo "Docker image built: $(DOCKER_IMAGE)"

docker-run: docker-build
	@echo "Running Docker container..."
	docker run --rm -it \
		-p 8080:8080 \
		$(DOCKER_IMAGE)

generate:
	@echo "Running code generation..."
	$(GO) generate ./...

generate-proto:
	@echo "Generating gRPC code from proto files..."
	@if ! command -v protoc > /dev/null; then \
		echo "protoc not found. Install Protocol Buffers compiler."; \
		exit 1; \
	fi
	@if ! command -v protoc-gen-go > /dev/null; then \
		echo "protoc-gen-go not found. Install with: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest"; \
		exit 1; \
	fi
	@if ! command -v protoc-gen-go-grpc > /dev/null; then \
		echo "protoc-gen-go-grpc not found. Install with: go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest"; \
		exit 1; \
	fi
	@mkdir -p api/proto/generated/audit/v1 api/proto/generated/auth/v1 api/proto/generated/authz/v1 api/proto/generated/identity/v1
	@protoc --go_out=api/proto/generated --go_opt=paths=source_relative \
		--go-grpc_out=api/proto/generated --go-grpc_opt=paths=source_relative \
		--proto_path=api/proto \
		api/proto/audit.proto
	@protoc --go_out=api/proto/generated --go_opt=paths=source_relative \
		--go-grpc_out=api/proto/generated --go-grpc_opt=paths=source_relative \
		--proto_path=api/proto \
		api/proto/auth.proto
	@protoc --go_out=api/proto/generated --go_opt=paths=source_relative \
		--go-grpc_out=api/proto/generated --go-grpc_opt=paths=source_relative \
		--proto_path=api/proto \
		api/proto/authz.proto
	@protoc --go_out=api/proto/generated --go_opt=paths=source_relative \
		--go-grpc_out=api/proto/generated --go-grpc_opt=paths=source_relative \
		--proto_path=api/proto \
		api/proto/identity.proto
	@echo "gRPC code generation complete"

verify: fmt-check lint test
	@echo "Verification complete"

# Install MkDocs and dependencies
docs-install:
	@echo "Installing MkDocs dependencies..."
	cd docs && pip install -r requirements.txt

# Serve documentation locally with auto-reload
docs-serve:
	@echo "Starting MkDocs development server..."
	@echo "Documentation will be available at http://127.0.0.1:8000"
	cd docs && mkdocs serve

# Build static documentation site
docs-build:
	@echo "Building static documentation site..."
	cd docs && mkdocs build
	@echo "Build complete! Output is in the 'docs/site/' directory"

# Deploy documentation to GitHub Pages
docs-deploy:
	@echo "Deploying documentation to GitHub Pages..."
	cd docs && mkdocs gh-deploy

# Clean build artifacts
docs-clean:
	@echo "Cleaning MkDocs build artifacts..."
	rm -rf docs/site/
	rm -rf docs/.mkdocs_cache/
	@echo "Clean complete!"

# Validate MkDocs configuration
docs-validate:
	@echo "Validating MkDocs configuration..."
	cd docs && mkdocs build --strict
	@echo "Configuration is valid!"

# Docker commands
docs-docker-build:
	@echo "Building Docker image for MkDocs..."
	cd docs && docker build -f Dockerfile -t goplt-docs:latest .

docs-docker-serve: docs-docker-build
	@echo "Starting MkDocs development server in Docker..."
	@echo "Documentation will be available at http://127.0.0.1:8000"
	cd docs && docker run --rm -it \
		-p 8000:8000 \
		-v "$$(pwd):/docs:ro" \
		goplt-docs:latest

docs-docker-build-site: docs-docker-build
	@echo "Building static documentation site in Docker..."
	cd docs && docker run --rm \
		-v "$$(pwd):/docs:ro" \
		-v "$$(pwd)/site:/docs/site" \
		goplt-docs:latest mkdocs build
	@echo "Build complete! Output is in the 'docs/site/' directory"

docs-docker-clean:
	@echo "Cleaning Docker images and containers..."
	-docker stop goplt-docs 2>/dev/null || true
	-docker rm goplt-docs 2>/dev/null || true
	-docker rmi goplt-docs:latest 2>/dev/null || true
	@echo "Clean complete!"

docs-docker-compose-up:
	@echo "Starting MkDocs server with docker-compose..."
	@echo "Documentation will be available at http://127.0.0.1:8000"
	cd docs && docker-compose -f docker-compose.yml up --build

docs-docker-compose-down:
	@echo "Stopping MkDocs server..."
	cd docs && docker-compose -f docker-compose.yml down

# Convenience aliases
docs: docs-serve
build-docs: docs-build
clean-docs: docs-clean
docs-docker: docs-docker-serve

