# Makefile for rhythm-backend
# Uses Maven wrapper (./mvnw)

# Load .env if exists (for DB credentials, etc.)
ifneq ($(wildcard .env),)
    include .env
    export
endif

DB_HOST ?= localhost
DB_PORT ?= 5432
DB_NAME ?= rhythm
DB_USERNAME ?= postgres
DB_PASSWORD ?= postgres

.PHONY: help install migrate codegen setup build run clean test

help:
	@echo "Available commands:"
	@echo "  make install        - Install Maven wrapper"
	@echo "  make migrate        - Run Flyway migrations"
	@echo "  make codegen        - Generate jOOQ classes from DB schema"
	@echo "  make setup          - Install wrapper + migrate + codegen (combined)"
	@echo "  make build          - Compile the project"
	@echo "  make run            - Run the application"
	@echo "  make clean          - Clean build artifacts"
	@echo "  make test           - Run tests"

install:
	./mvnw -v || mvn -v wrapper:wrapper -Dmaven=${MAVEN_VERSION:-3.9.9}

migrate:
	./mvnw flyway:migrate \
		-DDB_HOST=$(DB_HOST) \
		-DDB_PORT=$(DB_PORT) \
		-DDB_NAME=$(DB_NAME) \
		-DDB_USERNAME=$(DB_USERNAME) \
		-DDB_PASSWORD=$(DB_PASSWORD)

codegen:
	./mvnw jooq-codegen:generate \
		-DDB_HOST=$(DB_HOST) \
		-DDB_PORT=$(DB_PORT) \
		-DDB_NAME=$(DB_NAME) \
		-DDB_USERNAME=$(DB_USERNAME) \
		-DDB_PASSWORD=$(DB_PASSWORD)

setup: install migrate codegen

build:
	./mvnw compile

run:
	./mvnw spring-boot:run

clean:
	./mvnw clean

test:
	./mvnw test