# Makefile for Agentic RAG System
# Usage: make [target]

.PHONY: help install start start-bg stop restart status clean test test-unit test-integration dev-web dev-backend logs health port-check port-kill

# Default target
help:
	@echo "🚀 Agentic RAG System - Makefile Commands"
	@echo "========================================"
	@echo ""
	@echo "📦 Setup & Installation:"
	@echo "  make install         - Install all dependencies"
	@echo ""
	@echo "🚀 Service Management:"
	@echo "  make start           - Start backend service (foreground)"
	@echo "  make start-bg        - Start backend service (background)"
	@echo "  make stop            - Stop backend service"
	@echo "  make restart         - Restart backend service"
	@echo "  make status          - Check service status"
	@echo ""
	@echo "💻 Development:"
	@echo "  make dev-web         - Start frontend development server"
	@echo "  make dev-backend     - Start backend in development mode"
	@echo "  make dev             - Start both frontend and backend"
	@echo ""
	@echo "🧪 Testing:"
	@echo "  make test            - Run all tests"
	@echo "  make test-unit       - Run unit tests only"
	@echo "  make test-integration - Run integration tests only"
	@echo "  make test-e2e        - Run end-to-end tests"
	@echo ""
	@echo "🔧 Utilities:"
	@echo "  make logs            - Show service logs"
	@echo "  make health          - Check service health"
	@echo "  make port-check      - Check common development ports"
	@echo "  make port-kill       - Kill processes on common ports"
	@echo "  make clean           - Clean temporary files and caches"

# Installation
install:
	@echo "📦 Installing dependencies..."
	uv sync
	@echo "📦 Installing web dependencies..."
	cd web && npm install
	@echo "✅ All dependencies installed"

# Service management
start:
	@echo "🚀 Starting backend service in foreground..."
	@echo "💡 Use 'make start-bg' to run in background"
	@echo "⚠️  Press Ctrl+C to stop the service"
	./scripts/start_service.sh

start-bg:
	@echo "🚀 Starting backend service in background..."
	./scripts/start_service.sh --background

stop:
	@echo "🛑 Stopping backend service..."
	./scripts/stop_service.sh

restart: stop start

status:
	@echo "📊 Service Status:"
	@scripts/port_manager.sh check 8000

# Development
dev-web:
	@echo "💻 Starting web development server..."
	cd web && npm run dev

dev-backend:
	@echo "💻 Starting backend in development mode..."
	./scripts/start_service.sh --dev

dev:
	@echo "💻 Starting both frontend and backend for development..."
	@echo "Backend will start on http://localhost:8000"
	@echo "Frontend will start on http://localhost:3000"
	@make -j2 dev-backend dev-web

# Testing
test:
	@echo "🧪 Running all tests..."
	uv run pytest -v

test-unit:
	@echo "🧪 Running unit tests..."
	uv run pytest tests/unit/ -v

test-integration:
	@echo "🧪 Running integration tests..."
	uv run pytest tests/integration/ -v

test-e2e:
	@echo "🧪 Running end-to-end tests..."
	uv run python tests/integration/test_e2e_tool_ui.py

# Utilities
logs:
	@echo "📋 Service logs:"
	@if [ -f server.log ]; then tail -f server.log; else echo "No server.log found. Is the service running?"; fi

health:
	@echo "🏥 Checking service health..."
	@curl -s http://localhost:8000/health | jq . 2>/dev/null || curl -s http://localhost:8000/health || echo "❌ Service not responding"

port-check:
	@echo "🔍 Checking development ports..."
	@scripts/port_manager.sh check 3000
	@scripts/port_manager.sh check 3001
	@scripts/port_manager.sh check 8000

port-kill:
	@echo "💀 Killing processes on common development ports..."
	@scripts/port_manager.sh clear

clean:
	@echo "🧹 Cleaning temporary files..."
	rm -rf .pytest_cache
	rm -rf .tmp/*
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	rm -f server.log.* 2>/dev/null || true
	@echo "✅ Cleanup complete"

# Advanced targets
demo:
	@echo "🎭 Running demo workflow..."
	uv run python scripts/demo.py

api-docs:
	@echo "📖 Opening API documentation..."
	@echo "API docs available at: http://localhost:8000/docs"
	@command -v xdg-open >/dev/null && xdg-open http://localhost:8000/docs || echo "Open http://localhost:8000/docs in your browser"

web-url:
	@echo "🌐 Web interface available at: http://localhost:3000"
	@command -v xdg-open >/dev/null && xdg-open http://localhost:3000 || echo "Open http://localhost:3000 in your browser"

# Debug targets
debug-config:
	@echo "🔧 Configuration check:"
	@echo "Config file: $(shell ls -la config.yaml 2>/dev/null || echo 'Not found')"
	@echo "Virtual env: $(shell echo $$VIRTUAL_ENV || echo 'Not activated')"
	@echo "Python path: $(shell which python || echo 'Not found')"
	@echo "UV version: $(shell uv --version 2>/dev/null || echo 'Not installed')"

debug-deps:
	@echo "📦 Dependency status:"
	@echo "Backend dependencies:"
	@uv pip list | head -10
	@echo "Frontend dependencies:"
	@cd web && npm list --depth=0 | head -10

# Installation checks
check-install:
	@echo "✅ Checking installation..."
	@command -v uv >/dev/null || (echo "❌ uv not installed" && exit 1)
	@command -v node >/dev/null || (echo "❌ Node.js not installed" && exit 1)
	@command -v npm >/dev/null || (echo "❌ npm not installed" && exit 1)
	@[ -f config.yaml ] || (echo "❌ config.yaml not found" && exit 1)
	@[ -d .venv ] || (echo "❌ Virtual environment not found, run 'make install'" && exit 1)
	@echo "✅ All dependencies are installed"
