# Makefile for Geotechnical Layer Extractor
.PHONY: help setup single test-optimizer clean test info

.DEFAULT_GOAL := help

# Variables
PYTHON := python3
VENV := venv
VENV_BIN := $(VENV)/bin
PIP := $(VENV_BIN)/pip
PYTHON_VENV := $(VENV_BIN)/python

# Optimizer variables
EXAMPLES_DIR ?= examples
EXPECTED_DIR ?= output_man

help:
	@echo "🪨 Geotechnical Layer Extractor"
	@echo "================================"
	@echo ""
	@echo "📦 Setup:"
	@echo "  make setup         - Install all dependencies and create venv"
	@echo ""
	@echo "🚀 Running:"
	@echo "  make single PDF=   - Extract layers from a single PDF"
	@echo "  make test-optimizer     - Run prompt optimization on test PDFs"
	@echo ""
	@echo "🧹 Maintenance:"
	@echo "  make clean         - Clean all generated files"
	@echo "  make test          - Test Azure connections"
	@echo "  make info          - Show system information"
	@echo ""
	@echo "Examples:"
	@echo "  make single PDF=document.pdf"
	@echo "  make single PDF=examples/sample.pdf"
	@echo "  make test-optimizer"

# Create virtual environment
venv:
	@echo "🐍 Creating virtual environment..."
	@$(PYTHON) -m venv $(VENV)
	@echo "✅ Virtual environment created!"

# Setup - Install all dependencies
setup: venv
	@echo "📦 Installing dependencies..."
	@$(PIP) install --upgrade pip
	@$(PIP) install -r requirements.txt
	@mkdir -p output
	@mkdir -p output_man
	@mkdir -p examples
	@echo "✅ Setup complete!"
	@echo ""
	@echo "⚠️  Don't forget to:"
	@echo "1. Add your Azure keys in the scripts"
	@echo "2. Add test PDFs in examples/ folder"
	@echo "3. Add expected results in output_man/ folder"

# Extract layers from a single PDF
single:
	@if [ -z "$(PDF)" ]; then \
		echo "❌ Please provide a PDF file: make single PDF=your_file.pdf"; \
		exit 1; \
	fi
	@if [ ! -f "$(PDF)" ]; then \
		echo "❌ File not found: $(PDF)"; \
		exit 1; \
	fi
	@if [ ! -f "extract_single.py" ]; then \
		echo "❌ extract_single.py not found!"; \
		exit 1; \
	fi
	@echo "🚀 Extracting layers from: $(PDF)"
	@$(PYTHON_VENV) extract_single.py "$(PDF)" --pretty

# Run prompt optimizer
test-optimizer:
	@echo "🔬 Running Prompt Optimizer"
	@echo "=============================="
	@echo "📁 Examples directory: $(EXAMPLES_DIR)"
	@echo "📁 Expected results: $(EXPECTED_DIR)"
	@echo ""
	@if [ ! -f "prompt_optimizer.py" ]; then \
		echo "❌ prompt_optimizer.py not found!"; \
		exit 1; \
	fi
	@if [ ! -d "$(EXAMPLES_DIR)" ]; then \
		echo "❌ Examples directory not found: $(EXAMPLES_DIR)"; \
		echo "💡 Create it with: mkdir -p $(EXAMPLES_DIR)"; \
		exit 1; \
	fi
	@if [ ! -d "$(EXPECTED_DIR)" ]; then \
		echo "❌ Expected results directory not found: $(EXPECTED_DIR)"; \
		echo "💡 Create it with: mkdir -p $(EXPECTED_DIR)"; \
		exit 1; \
	fi
	@echo "🚀 Starting optimization..."
	@$(PYTHON_VENV) prompt_optimizer.py \
		--examples-dir $(EXAMPLES_DIR) \
		--expected-dir $(EXPECTED_DIR)

# Test Azure connections
test:
	@echo "🧪 Testing Azure connections..."
	@$(PYTHON_VENV) -c "from azure.ai.formrecognizer import DocumentAnalysisClient; \
		from azure.core.credentials import AzureKeyCredential; \
		from openai import AzureOpenAI; \
		print('✅ Azure imports successful')"
	@echo "✅ All imports working!"

# Clean all generated files
clean:
	@echo "🧹 Cleaning all files..."
	@rm -rf $(VENV)
	@rm -rf __pycache__ */__pycache__
	@rm -rf .pytest_cache
	@rm -rf *.pyc */*.pyc
	@rm -rf output/*.json output/*.txt output/*.html
	@rm -rf *.log
	@rm -rf test_results.json
	@find . -type f -name ".DS_Store" -delete 2>/dev/null || true
	@echo "✅ Clean complete!"

# Show system information
info:
	@echo "📊 System Information:"
	@echo "======================"
	@echo "OS: $$(uname -s)"
	@echo "Python: $$($$(which $(PYTHON)) --version)"
	@if [ -d "$(VENV)" ]; then \
		echo "Virtual env: ✅ $(VENV) exists"; \
		echo "Pip version: $$($(PIP) --version 2>/dev/null || echo 'N/A')"; \
	else \
		echo "Virtual env: ❌ Not created (run 'make setup')"; \
	fi
	@echo ""
	@echo "📁 Project structure:"
	@if [ -f "extract_single.py" ]; then \
		echo "✅ extract_single.py found"; \
	else \
		echo "❌ extract_single.py missing"; \
	fi
	@if [ -f "prompt_optimizer.py" ]; then \
		echo "✅ prompt_optimizer.py found"; \
	else \
		echo "❌ prompt_optimizer.py missing"; \
	fi
	@echo ""
	@echo "📁 Directories:"
	@if [ -d "output" ]; then \
		echo "✅ output/ exists ($$(ls -1 output/ 2>/dev/null | wc -l | tr -d ' ') files)"; \
	else \
		echo "❌ output/ not found"; \
	fi
	@if [ -d "$(EXAMPLES_DIR)" ]; then \
		echo "✅ $(EXAMPLES_DIR)/ exists ($$(ls -1 $(EXAMPLES_DIR)/*.pdf 2>/dev/null | wc -l | tr -d ' ') PDFs)"; \
	else \
		echo "❌ $(EXAMPLES_DIR)/ not found"; \
	fi
	@if [ -d "$(EXPECTED_DIR)" ]; then \
		echo "✅ $(EXPECTED_DIR)/ exists ($$(ls -1 $(EXPECTED_DIR)/*_layers.json 2>/dev/null | wc -l | tr -d ' ') JSON files)"; \
	else \
		echo "❌ $(EXPECTED_DIR)/ not found"; \
	fi