.PHONY: all clean dev-portal dev-landing test lint build vendor

# Node parameters
NPM=npm
YARN=yarn

# Go commands
GO=go

# Directories
PORTAL_DIR=portal
PORTAL_BACKEND_DIR=portal-backend
LANDING_DIR=landing-page

# Development server ports
PORTAL_FRONTEND_PORT=3000
PORTAL_BACKEND_PORT=8081
LANDING_PORT=3001

all: test build

clean:
	$(GO) clean
	rm -rf vendor/
	rm -rf $(PORTAL_DIR)/node_modules
	rm -rf $(PORTAL_BACKEND_DIR)/node_modules
	rm -rf $(LANDING_DIR)/node_modules

# Go targets
vendor:
	$(GO) mod vendor
	$(GO) mod tidy

# Dependencies
deps-go: vendor

deps-portal-frontend:
	cd $(PORTAL_DIR) && $(NPM) install

deps-portal-backend:
	cd $(PORTAL_BACKEND_DIR) && $(NPM) install

deps-landing:
	cd $(LANDING_DIR) && $(NPM) install

deps: deps-go deps-portal-frontend deps-portal-backend deps-landing

# Development commands
dev-portal-frontend:
	@echo "Starting web portal frontend in development mode..."
	cd $(PORTAL_DIR) && $(NPM) start

dev-portal-backend:
	@echo "Starting web portal backend in development mode..."
	cd $(PORTAL_BACKEND_DIR) && $(NPM) start

dev-portal: dev-portal-backend dev-portal-frontend
	@echo "Started web portal (frontend: $(PORTAL_FRONTEND_PORT), backend: $(PORTAL_BACKEND_PORT))"

dev-landing:
	@echo "Starting landing page in development mode..."
	cd $(LANDING_DIR) && $(NPM) start

# Build commands
build-portal-frontend:
	cd $(PORTAL_DIR) && $(NPM) run build

build-portal-backend:
	cd $(PORTAL_BACKEND_DIR) && $(NPM) run build

build-landing:
	cd $(LANDING_DIR) && $(NPM) run build

build: build-portal-frontend build-portal-backend build-landing

# Test commands
test-go: vendor
	$(GO) test -v ./... -coverprofile=coverage.out
	$(GO) tool cover -func=coverage.out
	$(GO) tool cover -html=coverage.out -o coverage.html

test-portal-frontend:
	cd $(PORTAL_DIR) && $(NPM) test

test-portal-backend:
	cd $(PORTAL_BACKEND_DIR) && $(NPM) test

test-landing:
	cd $(LANDING_DIR) && $(NPM) test

test: test-go test-portal-frontend test-portal-backend test-landing

# Lint commands
lint-portal-frontend:
	cd $(PORTAL_DIR) && $(NPM) run lint

lint-portal-backend:
	cd $(PORTAL_BACKEND_DIR) && $(NPM) run lint

lint-landing:
	cd $(LANDING_DIR) && $(NPM) run lint

lint: lint-portal-frontend lint-portal-backend lint-landing

# Development environment setup
dev-setup: deps
	@echo "Setting up development environment..."

# Run all web components locally
dev-all:
	@echo "Starting all web components in development mode..."
	$(MAKE) dev-portal & $(MAKE) dev-landing

# Help command
help:
	@echo "Available commands:"
	@echo "Development:"
	@echo "  make dev-portal           - Run the web portal (frontend + backend) locally"
	@echo "  make dev-portal-frontend  - Run only the portal frontend"
	@echo "  make dev-portal-backend   - Run only the portal backend"
	@echo "  make dev-landing          - Run the landing page locally"
	@echo "  make dev-all             - Run all web components locally"
	@echo ""
	@echo "Building:"
	@echo "  make build               - Build all components"
	@echo "  make build-portal-frontend - Build only the portal frontend"
	@echo "  make build-portal-backend  - Build only the portal backend"
	@echo "  make build-landing        - Build only the landing page"
	@echo ""
	@echo "Testing:"
	@echo "  make test                - Run all tests"
	@echo "  make test-portal-frontend - Run portal frontend tests"
	@echo "  make test-portal-backend  - Run portal backend tests"
	@echo "  make test-landing         - Run landing page tests"
	@echo ""
	@echo "Setup:"
	@echo "  make dev-setup          - Setup development environment"
	@echo "  make deps               - Install all dependencies"
	@echo "  make clean              - Clean build artifacts"

test-deps:
	docker-compose -f docker-compose.test.yml up -d

test-cleanup:
	docker-compose -f docker-compose.test.yml down

test-operator:
	$(MAKE) test-deps
	$(GO) test -v ./pkg/operator/... -coverprofile=operator-coverage.out
	$(GO) tool cover -func=operator-coverage.out
	$(GO) tool cover -html=operator-coverage.out -o operator-coverage.html
	$(MAKE) test-cleanup 