# Root Makefile: сборка UI, встраивание и сборка Go, запуск и Docker

PASS ?= devpass
PORT ?= 8888

UI_DIR := ui
BACK_DIR := back
PUBLIC_DIR := $(BACK_DIR)/cmd/public
BINARY := $(BACK_DIR)/knocker-serve

.PHONY: help ui-install ui-build embed-ui back-deps back-build run run-bg docker-build docker-up docker-down clean

help:
	@echo "Targets:"
	@echo "  ui-install   - npm install в $(UI_DIR)"
	@echo "  ui-build     - сборка Angular (prod)"
	@echo "  embed-ui     - копировать dist в $(PUBLIC_DIR)"
	@echo "  back-deps    - go mod tidy для бэкенда"
	@echo "  back-build   - сборка Go бинаря $(BINARY)"
	@echo "  run          - запуск сервера локально (PORT=$(PORT), PASS=$(PASS))"
	@echo "  run-bg       - запуск сервера в фоне (nohup)"
	@echo "  docker-build - сборка Docker образа через compose"
	@echo "  docker-up    - запуск контейнера (PORT=8888, PASS=$(PASS))"
	@echo "  docker-down  - остановить контейнер"
	@echo "  clean        - очистить встроенную статику и бинарь"

ui-install:
	cd $(UI_DIR) && npm install --no-audit --no-fund

ui-build:
	cd $(UI_DIR) && npm run build

embed-ui: ui-build
	rm -rf $(PUBLIC_DIR)
	mkdir -p $(PUBLIC_DIR)
	cp -r $(UI_DIR)/dist/project-front/browser/* $(PUBLIC_DIR)/
	rm -f $(PUBLIC_DIR)/media/Inter-*.woff2

back-deps:
	cd $(BACK_DIR) && go mod tidy

back-build: embed-ui back-deps
	cd $(BACK_DIR) && go build -o knocker-serve .

run: back-build
	cd $(BACK_DIR) && GO_KNOCKER_SERVE_PASS=$(PASS) GO_KNOCKER_SERVE_PORT=$(PORT) ./knocker-serve serve

run-bg: back-build
	cd $(BACK_DIR) && nohup env GO_KNOCKER_SERVE_PASS=$(PASS) GO_KNOCKER_SERVE_PORT=$(PORT) ./knocker-serve serve > /tmp/knocker.log 2>&1 & echo $$! && sleep 1 && tail -n +1 /tmp/knocker.log | sed -n '1,60p'

docker-build:
	docker compose build

docker-up:
	GO_KNOCKER_SERVE_PASS=$(PASS) docker compose up -d

docker-down:
	docker compose down

clean:
	rm -rf $(PUBLIC_DIR)
	rm -f $(BINARY)


