.PHONY: build run test clean docker-build docker-up docker-down migrate

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOTEST=$(GOCMD) test
GOCLEAN=$(GOCMD) clean
GOMOD=$(GOCMD) mod
BINARY_NAME=orchard-server
BINARY_PATH=./bin/$(BINARY_NAME)

# Build the application
build:
	@echo "Building $(BINARY_NAME)..."
	@mkdir -p ./bin
	$(GOBUILD) -o $(BINARY_PATH) -v ./cmd/orchard-server

# Run the application locally
run: build
	@echo "Running $(BINARY_NAME)..."
	$(BINARY_PATH)

# Run tests
test:
	@echo "Running tests..."
	$(GOTEST) -v ./...

# Clean build artifacts
clean:
	@echo "Cleaning..."
	$(GOCLEAN)
	rm -rf ./bin

# Download dependencies
deps:
	@echo "Downloading dependencies..."
	$(GOMOD) download
	$(GOMOD) tidy

# Build Docker image
docker-build:
	@echo "Building Docker image..."
	docker build -t orchard-server:latest .

# Start all services with Docker Compose
docker-up:
	@echo "Starting services..."
	docker-compose up -d

# Stop all services
docker-down:
	@echo "Stopping services..."
	docker-compose down

# View logs
docker-logs:
	docker-compose logs -f orchard-server

# Run database migrations manually
migrate:
	@echo "Running migrations..."
	docker-compose exec postgres psql -U orchard -d orchard -f /docker-entrypoint-initdb.d/001_initial.sql

# Development: Start dependencies only (db, minio, redis)
dev-deps:
	@echo "Starting development dependencies..."
	docker-compose up -d postgres minio minio-init redis

# Full rebuild and restart
rebuild: docker-down docker-build docker-up

# Show service status
status:
	docker-compose ps

# Initialize the S3 bucket
init-bucket:
	@echo "Initializing S3 bucket..."
	docker-compose up minio-init

# Help
help:
	@echo "Orchard Server Makefile"
	@echo ""
	@echo "Usage:"
	@echo "  make build        - Build the Go binary"
	@echo "  make run          - Build and run locally"
	@echo "  make test         - Run tests"
	@echo "  make clean        - Clean build artifacts"
	@echo "  make deps         - Download dependencies"
	@echo "  make docker-build - Build Docker image"
	@echo "  make docker-up    - Start all services"
	@echo "  make docker-down  - Stop all services"
	@echo "  make docker-logs  - View orchard-server logs"
	@echo "  make dev-deps     - Start only dependencies for local dev"
	@echo "  make rebuild      - Full rebuild and restart"
	@echo "  make status       - Show service status"
