# Go Backend Template Makefile

# Database configuration (update these for your project)
DB_USERNAME := postgres
DB_PASSWORD := postgres
DB_HOST := localhost
DB_PORT := 5432
DB_NAME := template_db

DB_URL = postgres://$(DB_USERNAME):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=disable

ifeq ($(OS),Windows_NT)
	DETECTED_OS := Windows
else
	DETECTED_OS := $(shell sh -c 'uname 2>/dev/null || echo Unknown')
endif

.SILENT: help
help:
	@echo
	@echo "Usage: make [command]"
	@echo
	@echo "Commands:"
	@echo " build                         Build the application"
	@echo " run                           Run the application"
	@echo " test                          Run unit tests"
	@echo " fmt                           Format source code"
	@echo
	@echo " migration-create name={name}  Create a new migration"
	@echo " migration-up                  Run all pending migrations"
	@echo " migration-down                Rollback last migration"
	@echo
	@echo " docker-up                     Start docker services"
	@echo " docker-down                   Stop docker services"
	@echo

# Build

.SILENT: build
build:
	@go build -o ./bin/server ./cmd/server/main.go
	@echo "✓ Binary built: ./bin/server"

# Run

.SILENT: run
run:
	@ENV_MODE=development go run cmd/server/main.go

# Test

.SILENT: test
test:
	@go test ./... -v

# Format

.SILENT: fmt
fmt:
	@go fmt ./...
	@echo "✓ Code formatted"

# Migrations

.SILENT: migration-create
migration-create:
	@if [ -z "$(name)" ]; then \
		echo "Error: name parameter is required"; \
		echo "Usage: make migration-create name=your_migration_name"; \
		exit 1; \
	fi
	@migrate create -ext sql -dir ./migrations -seq $(name)
	@echo "✓ Migration created: $(name)"

.SILENT: migration-up
migration-up:
	@migrate -database $(DB_URL) -path ./migrations up
	@echo "✓ Migrations applied"

.SILENT: migration-down
migration-down:
	@migrate -database $(DB_URL) -path ./migrations down 1
	@echo "✓ Last migration rolled back"

# Docker

.SILENT: docker-up
docker-up:
	@docker-compose up -d
	@echo "✓ Docker services started"

.SILENT: docker-down
docker-down:
	@docker-compose down
	@echo "✓ Docker services stopped"

# Dependencies

.SILENT: deps
deps:
	@go mod download
	@go mod tidy
	@echo "✓ Dependencies updated"

# Clean

.SILENT: clean
clean:
	@rm -rf ./bin
	@echo "✓ Build artifacts cleaned"

# Default

.DEFAULT_GOAL := help
