#!/bin/bash
# Development Environment Setup Script
# Sets up linting, validation, and pre-commit hooks for the homelab repository

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to log messages
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

log_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# Check if we're in the right directory
if [[ ! -f "README.md" ]] || [[ ! -d "hosts" ]]; then
    log_error "This script must be run from the homelab repository root directory"
    exit 1
fi

log_info "Setting up development environment for Homelab repository..."

# Step 1: Check Python installation
log_step "1. Checking Python installation..."
if ! command -v python3 &> /dev/null; then
    log_error "Python 3 is required but not installed"
    exit 1
fi
log_info "Python 3 found: $(python3 --version)"

# Step 2: Install Python dependencies
log_step "2. Installing Python dependencies..."
if [[ -f "requirements.txt" ]]; then
    python3 -m pip install --user -r requirements.txt
    log_info "Python dependencies installed"
else
    log_warn "requirements.txt not found, skipping Python dependencies"
fi

# Step 3: Install pre-commit hooks
log_step "3. Setting up pre-commit hooks..."
if command -v pre-commit &> /dev/null; then
    pre-commit install
    log_info "Pre-commit hooks installed"

    # Run pre-commit on all files to check setup
    log_info "Running initial pre-commit check (this may take a while)..."
    if pre-commit run --all-files; then
        log_info "All pre-commit checks passed!"
    else
        log_warn "Some pre-commit checks failed. This is normal for the first run."
        log_info "The hooks will now catch issues before commits."
    fi
else
    log_warn "pre-commit not found, skipping hook installation"
fi

# Step 4: Check Docker availability
log_step "4. Checking Docker availability..."
if command -v docker &> /dev/null; then
    log_info "Docker found: $(docker --version)"

    # Check if docker-compose is available
    if command -v docker-compose &> /dev/null; then
        log_info "Docker Compose found: $(docker-compose --version)"
    elif docker compose version &> /dev/null; then
        log_info "Docker Compose (plugin) found: $(docker compose version)"
    else
        log_warn "Docker Compose not found. Some validation features may not work."
    fi
else
    log_warn "Docker not found. Docker Compose validation will be skipped."
fi

# Step 5: Create .env file if it doesn't exist
log_step "5. Setting up environment configuration..."
if [[ ! -f ".env" ]]; then
    if [[ -f ".env.example" ]]; then
        cp .env.example .env
        log_info "Created .env file from template"
        log_warn "Please edit .env file with your actual configuration values"
    else
        log_warn ".env.example not found, skipping .env creation"
    fi
else
    log_info ".env file already exists"
fi

# Step 6: Test validation script
log_step "6. Testing validation script..."
if [[ -x "scripts/validate-compose.sh" ]]; then
    log_info "Testing Docker Compose validation on a sample file..."

    # Find a sample compose file to test
    sample_file=$(find hosts/ -name "*.yml" -o -name "*.yaml" | head -1)
    if [[ -n "$sample_file" ]]; then
        if ./scripts/validate-compose.sh "$sample_file"; then
            log_info "Validation script working correctly"
        else
            log_warn "Validation script test failed, but this may be expected"
        fi
    else
        log_warn "No sample compose files found for testing"
    fi
else
    log_warn "Validation script not found or not executable"
fi

# Step 7: Summary and next steps
log_step "7. Setup complete!"
echo
log_info "Development environment setup completed successfully!"
echo
echo -e "${BLUE}Next steps:${NC}"
echo "1. Edit .env file with your actual configuration values"
echo "2. Run 'yamllint hosts/' to check YAML files"
echo "3. Run './scripts/validate-compose.sh' to validate Docker Compose files"
echo "4. Make a test commit to see pre-commit hooks in action"
echo
echo -e "${BLUE}Available commands:${NC}"
echo "• yamllint hosts/                    - Lint YAML files"
echo "• ./scripts/validate-compose.sh      - Validate Docker Compose files"
echo "• pre-commit run --all-files         - Run all pre-commit checks"
echo "• pre-commit run --files <file>      - Run checks on specific files"
echo
log_info "Happy coding! 🚀"
