.PHONY: help build up down logs restart clean rebuild

help: ## Show this help message
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Available targets:'
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

build: ## Build the Docker images
	docker-compose build

up: ## Start all services
	docker-compose up -d

down: ## Stop all services
	docker-compose down

logs: ## View logs (use 'make logs service=scraper-google' for specific service)
	@if [ -z "$(service)" ]; then \
		docker-compose logs -f; \
	else \
		docker-compose logs -f $(service); \
	fi

restart: ## Restart all services
	docker-compose restart

restart-scraper: ## Restart just the scraper service
	docker-compose restart scraper-google

clean: ## Stop services and remove volumes (WARNING: deletes data)
	docker-compose down -v

rebuild: ## Rebuild and restart services
	docker-compose down
	docker-compose build --no-cache
	docker-compose up -d

ps: ## Show running containers
	docker-compose ps

stats: ## Show resource usage
	docker stats

exec-db: ## Connect to PostgreSQL database
	docker-compose exec postgres psql -U postgres -d jobs_scraper

backup-db: ## Backup the database
	@mkdir -p backups
	docker-compose exec postgres pg_dump -U postgres jobs_scraper > backups/backup_$(shell date +%Y%m%d_%H%M%S).sql
	@echo "Database backed up to backups/backup_$(shell date +%Y%m%d_%H%M%S).sql"

restore-db: ## Restore database from backup (use 'make restore-db file=backup.sql')
	@if [ -z "$(file)" ]; then \
		echo "Error: Please specify backup file. Usage: make restore-db file=backup.sql"; \
		exit 1; \
	fi
	docker-compose exec -T postgres psql -U postgres jobs_scraper < $(file)

shell-scraper: ## Open shell in scraper container
	docker-compose exec scraper-google sh

dev: ## Run in development mode with live logs
	docker-compose up --build

test: ## Run tests (if you have any)
	docker-compose exec scraper-google go test ./...
