# Go parameters
GOCMD = go
GOBUILD = $(GOCMD) build
GOCLEAN = $(GOCMD) clean
GOTEST = $(GOCMD) test
GOGET = $(GOCMD) get

# SQLite database filename
DB_FILE = db.sqlite3

# Directory containing migration SQL scripts
MIGRATIONS_DIR = internal/database/migrations
SAMPLE_DATA_DIR = internal/database/sample_data

# Build target
build:
	$(GOBUILD) -o bin/server main.go

# Run target
run: build
	./bin/server

watch: build
	watchexec -c -w . -r make run

# Clean target
clean:
	$(GOCLEAN)
	rm -rf bin
	rm -f db.sqlite3
	rm -f diagram*
	rm -f plantuml.jar
	rm -f erd.png
	rm -f config.toml

# Test target
test: db.sqlite3
	$(GOTEST) ./... -count=1

# Get dependencies target
deps:
	$(GOGET) -v ./...

# Update dependencies target
update:
	$(GOGET) -u -v ./...
	$(GOCMD) mod tidy

# Migration target
migrate:
	@echo "If this ever fails, run make clean and try again"
	@echo "Migrating database $(DB_FILE) using SQL scripts in $(MIGRATIONS_DIR)"
	@for file in $(wildcard $(MIGRATIONS_DIR)/*.sql); do \
		echo "Applying migration: $$file"; \
		sqlite3 $(DB_FILE) < $$file; \
	done

sampledata:
	@echo "If this ever fails, run make clean and try again"
	@echo "Migrating database $(DB_FILE) using SQL scripts in $(SAMPLE_DATA_DIR)"
	@for file in $(wildcard $(SAMPLE_DATA_DIR)/*.sql); do \
		echo "Applying migration: $$file"; \
		sqlite3 $(DB_FILE) < $$file; \
	done

# Target added primarily for CI/CD to ensure that the database is created before running tests
db.sqlite3:
	make migrate

dbdump:
	sqlite3 $(DB_FILE) .dump > database.txt

backup:
	mkdir -p backups
	sqlite3 $(DB_FILE) .dump | gzip -9 > ./backups/BACKUP_$(DB_FILE)_$(shell date +"%Y-%m-%d_%H:%M:%S").sql.gz
	# Restore with:
	# gzip -cd BACKUP_FILE.sql.gz | sqlite3 $(DB_FILE)

# Format
fmt:
	$(GOCMD) fmt ./...

# Lint
lint:
	golangci-lint run ./...
	
# Default target
default: build	

# Generate swagger docs
.PHONY: docs
docs:
	swag init -outputTypes go

.PHONY: docfmt
docfmt:
	swag fmt

# Generate ERD
# Requires eralchemy2
.PHONY: erd
erd:
	eralchemy2 -i sqlite:///db.sqlite3 -o erd.png

install-swag:
	@echo "Installing swag"
	@go get -u github.com/swaggo/swag/cmd/swag

# Convenience target to install golangci-lint
install-lint:
	@echo "Installing golangci-lint"
	@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.42.1

# Fetches the latest plantuml.jar and checks its SHA256 hash
plantuml.jar:
	curl -sSfL https://github.com/plantuml/plantuml/releases/download/v1.2024.3/plantuml.jar -o plantuml.jar \
	&& echo "519a4a7284c6a0357c369e4bb0caf72c4bfbbde851b8c6d6bbdb7af3c01fc82f plantuml.jar" | sha256sum -c

# Generate UML diagrams diagral.png & diagram.svg
.PHONY: uml
uml: plantuml.jar
	goplantuml -recursive . > diagram.puml
	java -jar plantuml.jar -tpng diagram.puml
	java -jar plantuml.jar -tsvg diagram.puml

# Convenience target to install just (requires sudo privileges)
install-just:
	@echo "Installing just"
	@curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash -s -- --to /usr/local/bin

.PHONY: types
types:
	tygo generate

.PHONY: install-golds
install-golds:
	go install go101.org/golds@latest

.PHONY: golds
golds:
	golds -port 6060 -nouses -plainsrc -wdpkgs-listing=promoted ./...
