# --- Variables ---
BINARY_NAME ?= niki
BUILD_DIR ?= bin

# ====================================================================================
# General Go Commands
# ====================================================================================
.PHONY: start test build clean mod-tidy lint install-linter help format swagger watch

start: build
	$(BUILD_DIR)/$(BINARY_NAME)

test:
	go test -v ./...

build:
	@mkdir -p $(BUILD_DIR)
	go build -o $(BUILD_DIR)/$(BINARY_NAME) main.go

clean:
	rm -rf $(BUILD_DIR)/

mod-tidy:
	go mod tidy

lint:
	golangci-lint run

install-linter:
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

format:
	@which gofumpt || (go install mvdan.cc/gofumpt@latest)
	@gofumpt -l -w .
	@which gci || (go install github.com/daixiang0/gci@latest)
	@gci write . --skip-generated --skip-vendor
	@golangci-lint run --fix

swagger:
	swag init

# Live Reload
watch:
	@if command -v CompileDaemon > /dev/null; then \
	    CompileDaemon -exclude-dir=.git -exclude=".#*" -command="./$(BUILD_DIR)/$(BINARY_NAME)"; \
	else \
	    read -p "Go's 'CompileDaemon' is not installed. Do you want to install it? [Y/n] " choice; \
	    if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \
	        go install github.com/githubnemo/CompileDaemon@latest; \
	        CompileDaemon -exclude-dir=.git -exclude=".#*" -command="./$(BUILD_DIR)/$(BINARY_NAME)"; \
	    else \
	        echo "You chose not to install CompileDaemon. Exiting..."; \
	        exit 1; \
	    fi; \
	fi

# ====================================================================================
# Database Migration Commands (legacy niki-core)
# ====================================================================================
.PHONY: migrate/status migrate/new migrate/up migrate/down

migrate/status:
	@sql-migrate status -env="production" -config=repository/mysql/dbconfig.yml

# Example: make migrate/new name=add_kindbox_permission
migrate/new:
	@echo 'Creating migration file for ${name}...'
	@sql-migrate new -env="production" -config=repository/mysql/dbconfig.yml ${name}

migrate/up: confirm
	@echo 'Running all migrations...'
	@sql-migrate up -env="production" -config=repository/mysql/dbconfig.yml

migrate/down: confirm
	@echo 'Tearing down last migration...'
	@sql-migrate down -env="production" -config=repository/mysql/dbconfig.yml -limit=1

confirm:
	@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]

# ====================================================================================
# Productapp Service Commands
# ====================================================================================
.PHONY: productapp-up productapp-up-logs

PRODUCTAPP_ENV = deploy/productapp/development/.env
PRODUCTAPP_COMPOSE = deploy/productapp/development/docker-compose.yml

productapp-up:
	@echo "Building and starting productapp full stack..."
	docker compose --env-file $(PRODUCTAPP_ENV) -f $(PRODUCTAPP_COMPOSE) up --build -d
	@echo "productapp stack is up."

productapp-up-logs:
	@echo "Building and starting productapp full stack..."
	docker compose --env-file $(PRODUCTAPP_ENV) -f $(PRODUCTAPP_COMPOSE) up --build

# ====================================================================================
# Docker Commands (legacy)
# ====================================================================================
.PHONY: docker

docker:
	docker compose up -d

# ====================================================================================
# Help Target
# ====================================================================================
help:
	@echo "Available targets:"
	@echo ""
	@echo "General Go Commands:"
	@echo "  start              - Build and run niki-core locally"
	@echo "  test               - Run tests"
	@echo "  build              - Compile binary"
	@echo "  clean              - Remove build artifacts"
	@echo "  mod-tidy           - Clean up dependencies"
	@echo "  format             - Format code (gofumpt + gci + lint fix)"
	@echo "  lint               - Run linters"
	@echo "  install-linter     - Install golangci-lint"
	@echo "  swagger            - Generate swagger docs"
	@echo "  watch              - Live reload with CompileDaemon"
	@echo ""
	@echo "Database Migration (niki-core):"
	@echo "  migrate/status     - Show migration status"
	@echo "  migrate/new        - Create new migration (name=<name>)"
	@echo "  migrate/up         - Run migrations up"
	@echo "  migrate/down       - Rollback last migration"
	@echo ""
	@echo "Productapp Service:"
	@echo "  productapp-up          - Build and start full stack in background"
	@echo "  productapp-up-logs     - Build and start full stack with logs"
	@echo ""
	@echo "Docker (legacy):"
	@echo "  docker             - Start docker-compose services"
