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

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod

# Node parameters
NPM=npm
YARN=yarn

# Directories
CMD_DIR=cmd
PKG_DIR=pkg
BUILD_DIR=build
PORTAL_DIR=web/portal
LANDING_DIR=web/landing

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

all: test build

clean:
	$(GOCLEAN)
	rm -f $(BUILD_DIR)/*
	rm -rf $(PORTAL_DIR)/frontend/node_modules
	rm -rf $(PORTAL_DIR)/backend/node_modules
	rm -rf $(LANDING_DIR)/node_modules

# Dependencies
deps-go:
	$(GOMOD) download
	$(GOMOD) tidy

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

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

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

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

# Development commands
dev-operator:
	@echo "Starting operator in development mode..."
	$(GOCMD) run $(CMD_DIR)/operator/main.go

dev-portal-frontend:
	@echo "Starting web portal frontend in development mode..."
	cd $(PORTAL_DIR)/frontend && $(NPM) run dev

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

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) run dev

# Build commands
build-operator:
	$(GOBUILD) -o $(BUILD_DIR)/operator $(CMD_DIR)/operator/main.go

build-portal-frontend:
	cd $(PORTAL_DIR)/frontend && $(NPM) run build

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

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

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

# Test commands
test-go:
	$(GOTEST) -v ./...

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

test-portal-backend:
	cd $(PORTAL_DIR)/backend && $(NPM) test

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

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

# Lint commands
lint-go:
	golangci-lint run

lint-portal-frontend:
	cd $(PORTAL_DIR)/frontend && $(NPM) run lint

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

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

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

# Docker commands
docker-build-operator:
	docker build -t container-mom-operator:local -f build/Dockerfile.operator .

docker-build-portal:
	docker build -t container-mom-portal:local -f build/Dockerfile.portal .

docker-build-landing:
	docker build -t container-mom-landing:local -f build/Dockerfile.landing .

# Development environment setup
dev-setup: deps
	@echo "Setting up development environment..."
	$(GOGET) -u golang.org/x/tools/cmd/goimports
	$(GOGET) -u github.com/golangci/golangci-lint/cmd/golangci-lint

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

# Help command
help:
	@echo "Available commands:"
	@echo "Development:"
	@echo "  make dev-operator         - Run the operator locally"
	@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 components locally"
	@echo ""
	@echo "Building:"
	@echo "  make build               - Build all components"
	@echo "  make build-operator      - Build only the operator"
	@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-go             - Run Go 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" 