// TODO: add commands for build and run in dev/produciton mode

ROOT=$(realpath $(dir $(lastword $(MAKEFILE_LIST))))

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

lint:
	which golangci-lint || (go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.0)
	golangci-lint run --config=$(ROOT)/.golangci.yml $(ROOT)/...

test:
	go test -v ./...

format:
	@which gofumpt || (go install mvdan.cc/gofumpt@latest)
	@gofumpt -l -w $(ROOT)
	@which gci || (go install github.com/daixiang0/gci@latest)
	@gci write $(ROOT)
	@which golangci-lint || (go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.0)
	@golangci-lint run --fix

build:
	go build main.go --migrate

run:
	go run main.go --migrate

docker:
	sudo docker compose up -d

swagger:
	swag init

# Live Reload
watch:
	@if command -v CompileDaemon > /dev/null; then \
	    CompileDaemon -exclude-dir=.git -exclude=".#*" -command="./niki"; \
	else \
	    read -p "Go's 'CompileDaemon' is not installed on your machine. 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="./niki"; \
	    else \
	        echo "You chose not to install CompileDaemon. Exiting..."; \
	        exit 1; \
	    fi; \
	fi

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