# Makefile
.PHONY: build

BINARY_NAME=oslstats

build:
	tailwindcss -i ./pkg/embedfs/files/css/input.css -o ./pkg/embedfs/files/css/output.css && \
	go mod tidy && \
   	templ generate && \
	go generate ./cmd/${BINARY_NAME} && \
	go build -ldflags="-w -s" -o ./bin/${BINARY_NAME}${SUFFIX} ./cmd/${BINARY_NAME}

run:
	make build
	./bin/${BINARY_NAME}${SUFFIX}

dev:
	templ generate --watch &\
	air &\
	tailwindcss -i ./pkg/embedfs/files/css/input.css -o ./pkg/embedfs/files/css/output.css --watch

clean:
	go clean

genenv:
	make build
	./bin/${BINARY_NAME} --genenv ${OUT}

envdoc:
	make build
	./bin/${BINARY_NAME} --envdoc

showenv:
	make build
	./bin/${BINARY_NAME} --showenv

# Database migration commands

# Run pending migrations (with automatic compressed backup)
migrate:
	@echo "Running migrations with automatic backup..."
	make build
	./bin/${BINARY_NAME}${SUFFIX} --migrate-up

# Run migrations without backup (dev only - faster)
migrate-no-backup:
	@echo "Running migrations WITHOUT backup (dev mode)..."
	make build
	./bin/${BINARY_NAME}${SUFFIX} --migrate-up --no-backup

# Rollback last migration group (with automatic backup)
migrate-rollback:
	@echo "Rolling back last migration group..."
	make build
	./bin/${BINARY_NAME}${SUFFIX} --migrate-rollback

# Show migration status
migrate-status:
	make build
	./bin/${BINARY_NAME}${SUFFIX} --migrate-status

# Preview migrations without applying (dry-run)
migrate-dry-run:
	@echo "Previewing pending migrations (dry-run)..."
	make build
	./bin/${BINARY_NAME}${SUFFIX} --migrate-dry-run

# Create new migration (usage: make migrate-create NAME=add_email_column)
migrate-create:
	@if [ -z "$(NAME)" ]; then \
		echo "❌ Error: NAME is required"; \
		echo ""; \
		echo "Usage: make migrate-create NAME=add_email_column"; \
		echo ""; \
		echo "Examples:"; \
		echo "  make migrate-create NAME=add_games_table"; \
		echo "  make migrate-create NAME=add_email_to_users"; \
		echo "  make migrate-create NAME=create_index_on_username"; \
		exit 1; \
	fi
	./bin/${BINARY_NAME}${SUFFIX} --migrate-create $(NAME)

# Reset database (DESTRUCTIVE - dev only!)
reset-db:
	@echo "⚠️  WARNING: This will DELETE ALL DATA!"
	make build
	./bin/${BINARY_NAME}${SUFFIX} --reset-db

.PHONY: migrate migrate-no-backup migrate-rollback migrate-status migrate-dry-run migrate-create reset-db
