.PHONY: help dev dev-full stop status logs build build-remote build-both test test-coverage lint typecheck e2e check clean install sync

help:
	@echo "Aeroflot.Flights.Web — Available commands:"
	@echo ""
	@echo "  Development:"
	@echo "    make dev          - Start Modern.js dev server (:8081)"
	@echo "    make dev-full     - Start dev server with API proxy (:8080)"
	@echo "    make stop         - Stop running dev server"
	@echo "    make status       - Check if dev server is running"
	@echo "    make logs         - View dev server logs (tail -f)"
	@echo ""
	@echo "  Building:"
	@echo "    make build        - Build standalone SSR server"
	@echo "    make build-remote - Build MF remote (mf-manifest.json)"
	@echo "    make build-both   - Build standalone + remote"
	@echo "    make clean        - Clean build artifacts"
	@echo ""
	@echo "  Testing & Quality:"
	@echo "    make test         - Run unit tests (Vitest)"
	@echo "    make test-coverage - Run tests with coverage"
	@echo "    make lint         - Lint code (ESLint)"
	@echo "    make typecheck    - Type check (TypeScript)"
	@echo "    make check        - Run typecheck + lint + test"
	@echo ""
	@echo "  E2E Testing:"
	@echo "    make e2e          - Run Playwright E2E tests"
	@echo ""
	@echo "  Deployment:"
	@echo "    make sync         - Sync files to flights-front repo"
	@echo ""
	@echo "  Setup:"
	@echo "    make install      - Install dependencies (pnpm install)"

PNPM := pnpm
PID_FILE := .dev.pid
LOG_FILE := .dev.log

# Development
dev:
	@echo "Starting Modern.js dev server in background..."
	@nohup $(PNPM) dev > $(LOG_FILE) 2>&1 & echo $$! > $(PID_FILE)
	@echo "Dev server started (PID: $$(cat $(PID_FILE)))"
	@echo "View logs: make logs"

dev-full:
	@echo "Starting dev server with API proxy in background..."
	@nohup $(PNPM) dev:full > $(LOG_FILE) 2>&1 & echo $$! > $(PID_FILE)
	@echo "Dev server started (PID: $$(cat $(PID_FILE)))"
	@echo "  App:  http://localhost:8080"
	@echo "  API:  http://localhost:8080/api/*"
	@echo "View logs: make logs"

stop:
	@echo "Stopping dev server..."
	@if [ -f $(PID_FILE) ]; then \
		kill $$(cat $(PID_FILE)) 2>/dev/null || true; \
		rm -f $(PID_FILE); \
	fi
	@pkill -f "modern dev" 2>/dev/null || true
	@pkill -f "node scripts/dev-server" 2>/dev/null || true
	@lsof -ti:8080 -ti:8081 2>/dev/null | xargs kill 2>/dev/null || true
	@echo "Stopped"

status:
	@if [ -f $(PID_FILE) ] && ps -p $$(cat $(PID_FILE)) > /dev/null 2>&1; then \
		echo "Dev server is running (PID: $$(cat $(PID_FILE)))"; \
	else \
		rm -f $(PID_FILE) 2>/dev/null; \
		echo "Dev server is not running"; \
	fi

logs:
	@if [ -f $(LOG_FILE) ]; then \
		tail -f $(LOG_FILE); \
	else \
		echo "No log file. Start server with: make dev"; \
	fi

# Building
build:
	$(PNPM) build:standalone

build-remote:
	$(PNPM) build:remote

build-both:
	$(PNPM) build:both

clean:
	rm -rf dist/
	@echo "Clean complete"

# Testing & Quality
test:
	$(PNPM) test

test-coverage:
	$(PNPM) test:coverage

lint:
	$(PNPM) lint

typecheck:
	$(PNPM) typecheck

check: typecheck lint test

# E2E
e2e:
	$(PNPM) test:e2e

# Deployment
sync:
	./scripts/sync-to-flights-front.sh

# Setup
install:
	$(PNPM) install
