# Jobs Scraper Monorepo Makefile

# Variables
APP_NAME=jobs-scraper
API_DIR=./services/api
SCRAPER_LINKEDIN_DIR=./services/scraper-linkedin
SCRAPER_GLASSDOOR_DIR=./services/scraper-glassdoor
SCRAPER_PLAYWRIGHT_DIR=./services/scraper-playwright
CRON_ANALYZER_DIR=./apps/cron-analyzer
CV_ANALYZER_DIR=./apps/cv-analyzer
DOCS_DIR=./services/api/pkg/swagger
BIN_DIR=./bin
API_MAIN_FILE=$(API_DIR)/cmd/server/main.go
SCRAPER_LINKEDIN_MAIN_FILE=$(SCRAPER_LINKEDIN_DIR)/main.go
CRON_MAIN_FILE=$(CRON_ANALYZER_DIR)/cmd/cli/main.go
CV_MAIN_FILE=$(CV_ANALYZER_DIR)/cmd/cli/main.go

# Default target
.DEFAULT_GOAL := help

# RabbitMQ server
rabbitmq-server:
	@echo "Starting RabbitMQ server..."
	docker run --rm -p 5672:5672 -p 15672:15672 rabbitmq:3-management

# Services
.PHONY: run-api
run-api: ## Run the API service
	@echo "Running $(APP_NAME) API service..."
	@cd $(API_DIR) && go run cmd/server/main.go

.PHONY: build-api
build-api: ## Build the API service
	@echo "Building API service..."
	@cd $(API_DIR) && go build -o bin/api cmd/server/main.go

.PHONY: run-scraper-linkedin
run-scraper-linkedin: ## Run the LinkedIn scraper service
	@echo "Running LinkedIn scraper service..."
	@cd $(SCRAPER_LINKEDIN_DIR) && go run main.go

.PHONY: build-scraper-linkedin
build-scraper-linkedin: ## Build the LinkedIn scraper service
	@echo "Building LinkedIn scraper service..."
	@cd $(SCRAPER_LINKEDIN_DIR) && go build -o bin/scraper main.go

.PHONY: run-scraper-glassdoor
run-scraper-glassdoor: ## Run the Glassdoor scraper service
	@echo "Running Glassdoor scraper service..."
	@cd $(SCRAPER_GLASSDOOR_DIR) && npm run dev

.PHONY: build-scraper-glassdoor
build-scraper-glassdoor: ## Build the Glassdoor scraper service
	@echo "Building Glassdoor scraper service..."
	@cd $(SCRAPER_GLASSDOOR_DIR) && npm run build

.PHONY: setup-scraper-playwright
setup-scraper-playwright: ## Setup the Playwright scraper service
	@echo "Setting up Playwright scraper service..."
	@cd $(SCRAPER_PLAYWRIGHT_DIR) && bash setup.sh

.PHONY: run-scraper-playwright
run-scraper-playwright: ## Run the Playwright scraper service
	@echo "Running Playwright scraper service..."
	@cd $(SCRAPER_PLAYWRIGHT_DIR) && source venv/bin/activate && python playwright_worker.py

.PHONY: docker-scraper-playwright
docker-scraper-playwright: ## Build and run Playwright scraper in Docker
	@echo "Building Docker image for Playwright scraper..."
	@cd $(SCRAPER_PLAYWRIGHT_DIR) && docker build -t playwright-scraper .
	@echo "Running Playwright scraper in Docker..."
	@docker run --rm --name playwright-scraper playwright-scraper

# Apps
.PHONY: run-cron-analyzer
run-cron-analyzer: ## Run the job analysis cron service
	@echo "Starting Job Analysis Cron Service..."
	@cd $(CRON_ANALYZER_DIR) && go run cmd/cli/main.go

.PHONY: build-cron-analyzer
build-cron-analyzer: ## Build the cron analyzer app
	@echo "Building cron analyzer app..."
	@cd $(CRON_ANALYZER_DIR) && go build -o bin/cron-analyzer cmd/cli/main.go

.PHONY: run-cv-analyzer
run-cv-analyzer: ## Run the CV analyzer app
	@echo "Running CV analyzer app..."
	@cd $(CV_ANALYZER_DIR) && go run cmd/cli/main.go

.PHONY: build-cv-analyzer
build-cv-analyzer: ## Build the CV analyzer app
	@echo "Building CV analyzer app..."
	@cd $(CV_ANALYZER_DIR) && go build -o bin/cv-analyzer cmd/cli/main.go

.PHONY: run-openai-demo
run-openai-demo: ## Run the OpenAI service demo
	@echo "Running OpenAI service demo..."
	@cd $(CRON_ANALYZER_DIR) && go run cmd/openai-demo/main.go

.PHONY: build-openai-demo
build-openai-demo: ## Build the OpenAI service demo
	@echo "Building OpenAI service demo..."
	@cd $(CRON_ANALYZER_DIR) && go build -o bin/openai-demo cmd/openai-demo/main.go

# Legacy aliases for backward compatibility
.PHONY: run
run: run-api ## Alias for run-api

.PHONY: scraper
scraper: run-scraper-linkedin ## Alias for run-scraper-linkedin

.PHONY: cron
cron: run-cron-analyzer ## Alias for run-cron-analyzer

.PHONY: migrate
migrate: ## Run database migrations
	@echo "Running database migrations..."
	@go run $(MIGRATE_MAIN_FILE)

.PHONY: clean
clean: ## Clean build artifacts
	@echo "Cleaning build artifacts..."
	@rm -rf $(BIN_DIR)
	@rm -f $(SCRAPER_DIR)/scraper
	@echo "Clean complete"

.PHONY: help
help: ## Show this help message
	@echo "Jobs Scraper Monorepo - Available commands:"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2}'

# Swagger documentation
.PHONY: swagger
swagger: ## Generate Swagger documentation
	@echo "Generating Swagger documentation..."
	@go run github.com/swaggo/swag/cmd/swag@latest init -g $(API_MAIN_FILE) -o $(DOCS_DIR)
	@echo "Swagger documentation generated in $(DOCS_DIR)/"

# Docker operations (if needed in the future)
.PHONY: docker-build
docker-build: ## Build Docker image
	@echo "Building Docker image..."
	@docker build -t $(APP_NAME) .
	@echo "Docker image built: $(APP_NAME)"

.PHONY: docker-run
docker-run: ## Run Docker container
	@echo "Running Docker container..."
	@docker run -p 8080:8080 $(APP_NAME)

