# Nicepayments Web Application Makefile

# Variables
NODE_VERSION = 20
DOCKER_REGISTRY = harbor.jongyeob.com
DOCKER_PROJECT = nicepay
DOCKER_IMAGE = nice-app-web
DOCKER_TAG = latest
DOCKER_FULL_IMAGE = $(DOCKER_REGISTRY)/$(DOCKER_PROJECT)/$(DOCKER_IMAGE)
CONTAINER_NAME = nice-app-web
PORT = 80
PLATFORMS = linux/amd64,linux/arm64

# Default target
.DEFAULT_GOAL := help

# Help target
help: ## Show this help message
	@echo "Available targets:"
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

# Development targets
install: ## Install dependencies
	pnpm install

dev: ## Start development server
	pnpm run dev

build: ## Build for production
	pnpm run build

preview: ## Preview production build
	pnpm run preview

lint: ## Run linting
	pnpm run lint

clean: ## Clean node_modules, dist and Docker artifacts
	rm -rf node_modules dist .pnpm-store
	docker system prune -f

# Docker Setup
setup: ## Setup Docker buildx for multi-platform builds
	docker buildx create --name nice-builder --driver docker-container --use || docker buildx use nice-builder
	docker buildx inspect --bootstrap

# Docker Build targets
docker-build: setup ## Build multi-platform Docker image locally
	docker buildx build --platform $(PLATFORMS) \
		-f docker/Dockerfile \
		-t $(DOCKER_IMAGE):$(DOCKER_TAG) \
		-t $(DOCKER_IMAGE):latest \
		--load .

docker-push: setup ## Build and push multi-platform image to registry
	docker buildx build --platform $(PLATFORMS) \
		-f docker/Dockerfile \
		-t $(DOCKER_FULL_IMAGE):$(DOCKER_TAG) \
		-t $(DOCKER_FULL_IMAGE):latest \
		--push .

# Docker Runtime targets
docker-run: ## Run Docker container with Apache
	docker stop $(CONTAINER_NAME) 2>/dev/null || true
	docker rm $(CONTAINER_NAME) 2>/dev/null || true
	docker run -d \
		--name $(CONTAINER_NAME) \
		--restart unless-stopped \
		-p $(PORT):80 \
		$(DOCKER_IMAGE):$(DOCKER_TAG)
	@echo "Container running at http://localhost:$(PORT)"

docker-stop: ## Stop and remove Docker container
	docker stop $(CONTAINER_NAME) || true
	docker rm $(CONTAINER_NAME) || true

docker-restart: docker-stop docker-run ## Restart Docker container

docker-logs: ## Show Docker container logs
	docker logs -f $(CONTAINER_NAME)

docker-shell: ## Access Docker container shell
	docker exec -it $(CONTAINER_NAME) /bin/sh

docker-clean: ## Clean all Docker resources
	docker stop $(CONTAINER_NAME) 2>/dev/null || true
	docker rm $(CONTAINER_NAME) 2>/dev/null || true
	docker rmi $(DOCKER_IMAGE):$(DOCKER_TAG) 2>/dev/null || true
	docker rmi $(DOCKER_FULL_IMAGE):$(DOCKER_TAG) 2>/dev/null || true
	docker buildx rm nice-builder 2>/dev/null || true

# Registry targets
login: ## Login to Harbor registry
	docker login $(DOCKER_REGISTRY)

pull: ## Pull Docker image from Harbor
	docker pull $(DOCKER_FULL_IMAGE):$(DOCKER_TAG)
	docker tag $(DOCKER_FULL_IMAGE):$(DOCKER_TAG) $(DOCKER_IMAGE):$(DOCKER_TAG)

# Docker Compose targets
compose-up: ## Start services with docker-compose
	docker-compose -f docker/docker-compose.yml up -d

compose-down: ## Stop services with docker-compose
	docker-compose -f docker/docker-compose.yml down

compose-restart: ## Restart services with docker-compose
	docker-compose -f docker/docker-compose.yml restart

compose-logs: ## Show docker-compose logs
	docker-compose -f docker/docker-compose.yml logs -f

# Deployment targets
deploy: build docker-push ## Build app and deploy to registry

deploy-tag: ## Deploy with custom tag (use: make deploy-tag TAG=v1.0.0)
	docker buildx build --platform $(PLATFORMS) \
		-f docker/Dockerfile \
		-t $(DOCKER_FULL_IMAGE):$(TAG) \
		--push .

# Development with Docker
dev-docker: ## Run development server in Docker
	docker run --rm -it \
		-v $(PWD):/app \
		-p 5173:5173 \
		-w /app \
		node:20-alpine \
		sh -c "npm install -g pnpm && pnpm install && pnpm run dev --host 0.0.0.0"

# Testing targets
test: ## Run tests in Docker
	docker run --rm \
		-v $(PWD):/app \
		-w /app \
		node:20-alpine \
		sh -c "npm install -g pnpm && pnpm install && pnpm test"

# Utility targets
info: ## Show Docker and build information
	@echo "Docker Registry: $(DOCKER_REGISTRY)"
	@echo "Docker Image: $(DOCKER_FULL_IMAGE):$(DOCKER_TAG)"
	@echo "Platforms: $(PLATFORMS)"
	@echo "Container Name: $(CONTAINER_NAME)"
	@echo "Port: $(PORT)"
	@docker version --format 'Docker Version: {{.Server.Version}}'
	@docker buildx version

status: ## Show container status
	@docker ps --filter name=$(CONTAINER_NAME) --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

inspect: ## Inspect the built image
	docker buildx imagetools inspect $(DOCKER_FULL_IMAGE):$(DOCKER_TAG)

# Quick commands
quick-build: setup docker-build docker-run ## Quick build and run locally
quick-deploy: setup build docker-push ## Quick deploy to registry

.PHONY: help install dev build preview lint clean setup docker-build docker-push docker-run docker-stop docker-restart docker-logs docker-shell docker-clean login pull compose-up compose-down compose-restart compose-logs deploy deploy-tag dev-docker test info status inspect quick-build quick-deploy