.PHONY: build clean test lint docker-build docker-push run help install init

# Variables
APP_NAME := gitea-webhook-ambassador
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
PYTHON := python3
PIP := pip
VENV := venv
CONFIG_FILE := config.yaml

# Python commands
PYTHON_FILES := $(shell find . -name "*.py" -type f)
REQUIREMENTS := requirements.txt

# Default target
.DEFAULT_GOAL := help

# Install dependencies
install:
	@echo "Installing dependencies..."
	$(PYTHON) -m venv $(VENV)
	. $(VENV)/bin/activate && $(PIP) install -r $(REQUIREMENTS)

# Initialize database
init:
	@echo "Initializing database..."
	. $(VENV)/bin/activate && $(PYTHON) -c "from app.models.database import create_tables; create_tables(); print('Database initialized')"

# Build (for Python, this means installing dependencies)
build: install
	@echo "Python application ready"

# Clean build artifacts
clean:
	@echo "Cleaning up..."
	@rm -rf $(VENV)
	@rm -f *.db
	@rm -rf logs/
	@rm -f *.pid

# Run tests
test:
	@echo "Running tests..."
	. $(VENV)/bin/activate && $(PYTHON) test_enhanced_features.py

# Run linter
lint:
	@echo "Running linter..."
	. $(VENV)/bin/activate && flake8 app/ --max-line-length=120 --ignore=E501,W503

# Build Docker image
docker-build:
	@echo "Building Docker image $(APP_NAME):$(VERSION)..."
	docker build -t $(APP_NAME):$(VERSION) .
	docker tag $(APP_NAME):$(VERSION) $(APP_NAME):latest

# Push Docker image to registry
docker-push: docker-build
	@echo "Pushing Docker image $(APP_NAME):$(VERSION)..."
	docker push $(APP_NAME):$(VERSION)
	docker push $(APP_NAME):latest

# Run locally
run: build init
	@echo "Starting $(APP_NAME)..."
	. $(VENV)/bin/activate && $(PYTHON) -m uvicorn app.main_enhanced:app --host 0.0.0.0 --port 8000

# Run in background
start: build init
	@echo "Starting $(APP_NAME) in background..."
	. $(VENV)/bin/activate && nohup $(PYTHON) -m uvicorn app.main_enhanced:app --host 0.0.0.0 --port 8000 > logs/service.log 2>&1 &
	@echo $$! > service.pid
	@echo "Service started with PID $$(cat service.pid)"

# Stop service
stop:
	@if [ -f service.pid ]; then \
		echo "Stopping $(APP_NAME)..."; \
		kill $$(cat service.pid) 2>/dev/null || true; \
		rm -f service.pid; \
		echo "Service stopped"; \
	else \
		echo "No service.pid found"; \
	fi

# Restart service
restart: stop start

# Show service status
status:
	@if [ -f service.pid ]; then \
		PID=$$(cat service.pid); \
		if ps -p $$PID > /dev/null 2>&1; then \
			echo "✅ $(APP_NAME) is running (PID: $$PID)"; \
			echo "📝 Log file: logs/service.log"; \
			echo "🌐 Access: http://localhost:8000"; \
		else \
			echo "❌ $(APP_NAME) is not running (PID file exists but process not found)"; \
			rm -f service.pid; \
		fi; \
	else \
		echo "❌ $(APP_NAME) is not running"; \
	fi

# Show logs
logs:
	@if [ -f logs/service.log ]; then \
		echo "📝 Latest logs (last 50 lines):"; \
		echo "----------------------------------------"; \
		tail -n 50 logs/service.log; \
		echo "----------------------------------------"; \
	else \
		echo "❌ No log file found"; \
	fi

# Follow logs
follow:
	@if [ -f logs/service.log ]; then \
		echo "📝 Following logs (Ctrl+C to exit):"; \
		tail -f logs/service.log; \
	else \
		echo "❌ No log file found"; \
	fi

# Show help
help:
	@echo "Gitea Webhook Ambassador (Python) - Makefile commands:"
	@echo "  install       - Install Python dependencies"
	@echo "  init          - Initialize database"
	@echo "  build         - Install dependencies (alias for install)"
	@echo "  clean         - Remove build artifacts and logs"
	@echo "  test          - Run tests"
	@echo "  lint          - Run linter"
	@echo "  docker-build  - Build Docker image"
	@echo "  docker-push   - Build and push Docker image to registry"
	@echo "  run           - Install, init and run locally (foreground)"
	@echo "  start         - Install, init and start in background"
	@echo "  stop          - Stop background service"
	@echo "  restart       - Restart background service"
	@echo "  status        - Show service status"
	@echo "  logs          - Show latest logs"
	@echo "  follow        - Follow logs in real-time"
	@echo "  help          - Show this help message" 