.PHONY: build run test clean deps docker-build docker-run docker-compose-up docker-compose-down

# Variables
BINARY_NAME=support
BINARY_UNIX=$(BINARY_NAME)_unix
VERSION?=1.0.0
BUILD_TIME?=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
COMMIT?=$(shell git rev-parse --short HEAD)

# Go build flags
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.Commit=$(COMMIT)"

# Default target
all: deps build

# Install dependencies
deps:
	go mod download
	go mod tidy

# Build the application
build:
	go build $(LDFLAGS) -o $(BINARY_NAME) cmd/main.go

# Build for Linux
build-linux:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BINARY_UNIX) cmd/main.go

# Run the application
run:
	go run cmd/main.go

# Run tests
test:
	go test -v ./...

# Run tests with coverage
test-coverage:
	go test -v -cover ./...

# Clean build artifacts
clean:
	go clean
	rm -f $(BINARY_NAME)
	rm -f $(BINARY_UNIX)

# Build Docker image
docker-build:
	docker build -t $(BINARY_NAME):$(VERSION) .

# Run Docker container
docker-run:
	docker run -p 8080:8080 $(BINARY_NAME):$(VERSION)

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

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

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

# Run database migrations
migrate-up:
	go run cmd/migrate.go up

# Rollback database migrations
migrate-down:
	go run cmd/migrate.go down

# Seed database
seed-db:
	go run cmd/seed.go

# Format code
fmt:
	go fmt ./...

# Lint code
lint:
	golangci-lint run

# Security check
security:
	gosec ./...

# Generate API documentation
docs:
	swag init -g cmd/main.go

# Install development tools
install-tools:
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	go install github.com/securecodewarrior/gosec/v2/cmd/gosec@latest
	go install github.com/swaggo/swag/cmd/swag@latest

# Help target
help:
	@echo "Available targets:"
	@echo "  all          - Install dependencies and build the application"
	@echo "  deps         - Install dependencies"
	@echo "  build        - Build the application"
	@echo "  build-linux  - Build the application for Linux"
	@echo "  run          - Run the application"
	@echo "  test         - Run tests"
	@echo "  test-coverage - Run tests with coverage"
	@echo "  clean        - Clean build artifacts"
	@echo "  docker-build - Build Docker image"
	@echo "  docker-run   - Run Docker container"
	@echo "  docker-compose-up - Start Docker Compose"
	@echo "  docker-compose-down - Stop Docker Compose"
	@echo "  docker-compose-logs - View Docker Compose logs"
	@echo "  migrate-up   - Run database migrations"
	@echo "  migrate-down - Rollback database migrations"
	@echo "  seed-db      - Seed database"
	@echo "  fmt          - Format code"
	@echo "  lint         - Lint code"
	@echo "  security     - Security check"
	@echo "  docs         - Generate API documentation"
	@echo "  install-tools - Install development tools"
	@echo "  help         - Show this help message"