# MTG Search - Makefile for common development tasks

.PHONY: help build test run clean docker-build docker-run docker-stop frontend-install frontend-build backend-build

help:
	@echo "MTG Search - Available Commands"
	@echo "================================"
	@echo "make build              - Build both backend and frontend"
	@echo "make backend-build      - Build backend only"
	@echo "make frontend-build     - Build frontend only"
	@echo "make test               - Run all tests"
	@echo "make backend-test       - Run backend tests"
	@echo "make frontend-test      - Run frontend tests"
	@echo "make run                - Run backend server (requires frontend built)"
	@echo "make dev                - Run frontend dev server and backend"
	@echo "make clean              - Clean all build directories"
	@echo "make docker-build       - Build Docker image"
	@echo "make docker-run         - Start services with docker-compose"
	@echo "make docker-stop        - Stop all docker-compose services"
	@echo "make docker-clean       - Remove all containers and volumes"
	@echo "make lint               - Run code quality checks"
	@echo "make format             - Format code"
	@echo "make db-init            - Initialize database"

build: backend-build frontend-build
	@echo "✓ Build complete"

backend-build:
	@echo "Building backend..."
	cd backend && ../gradlew clean build -x test
	@echo "✓ Backend build complete"

frontend-build:
	@echo "Building frontend..."
	cd frontend && npm install && npm run build
	@echo "✓ Frontend build complete"

frontend-install:
	@echo "Installing frontend dependencies..."
	cd frontend && npm install
	@echo "✓ Frontend dependencies installed"

test: backend-test
	@echo "✓ All tests passed"

backend-test:
	@echo "Running backend tests..."
	cd backend && ../gradlew test

frontend-test:
	@echo "Running frontend tests..."
	cd frontend && npm run test

run:
	@echo "Starting backend server..."
	cd backend && ../gradlew bootRun

dev: frontend-install
	@echo "Starting development environment..."
	@echo "Frontend: http://localhost:5173"
	@echo "Backend: http://localhost:8080"
	@echo ""
	@trap 'kill %1 %2' SIGINT; \
	(cd frontend && npm run dev) & \
	(cd backend && ../gradlew bootRun) & \
	wait

clean:
	@echo "Cleaning build directories..."
	cd backend && ../gradlew clean
	cd frontend && rm -rf node_modules dist
	rm -rf logs
	@echo "✓ Clean complete"

docker-build:
	@echo "Building Docker image..."
	docker build -t mtgsearch:latest .
	@echo "✓ Docker build complete"

docker-run:
	@echo "Starting Docker services..."
	docker-compose up --build
	@echo "✓ Services started"

docker-stop:
	@echo "Stopping Docker services..."
	docker-compose down
	@echo "✓ Services stopped"

docker-clean:
	@echo "Cleaning Docker resources..."
	docker-compose down -v
	docker image rm mtgsearch:latest
	@echo "✓ Docker cleanup complete"

lint:
	@echo "Running linters..."
	cd backend && ../gradlew check -x test
	cd frontend && npm run lint
	@echo "✓ Linting complete"

format:
	@echo "Formatting code..."
	cd frontend && npm run lint
	@echo "✓ Formatting complete"

db-init:
	@echo "Initializing database..."
	docker run --name mtgsearch-postgres \
		-e POSTGRES_DB=mtgsearch \
		-e POSTGRES_USER=postgres \
		-e POSTGRES_PASSWORD=postgres \
		-p 5432:5432 \
		-d postgres:16-alpine
	@echo "✓ Database initialized on localhost:5432"

.DEFAULT_GOAL := help
