#!/bin/bash

# Build documentation with logo assets for RUSTELO
# This script generates cargo documentation and copies logo assets to the output directory

set -e

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

# Function to print colored output
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

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

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

# Check if we're in the correct directory
if [ ! -f "Cargo.toml" ]; then
    print_error "Cargo.toml not found. Please run this script from the project root directory."
    exit 1
fi

if [ ! -d "logos" ]; then
    print_error "logos directory not found. Please ensure the logos directory exists in the project root."
    exit 1
fi

print_status "Building RUSTELO documentation with logo assets..."

# Clean previous documentation build
print_status "Cleaning previous documentation..."
cargo clean --doc

# Build documentation
print_status "Generating cargo documentation..."
if cargo doc --no-deps --lib --workspace --document-private-items; then
    print_status "Documentation generated successfully"
else
    print_error "Failed to generate documentation"
    exit 1
fi

# Copy logo assets to documentation output
print_status "Copying logo assets to documentation output..."
if [ -d "target/doc" ]; then
    cp -r logos target/doc/
    print_status "Logo assets copied to target/doc/logos/"
else
    print_error "Documentation output directory not found"
    exit 1
fi

# Check if logos were copied successfully
if [ -d "target/doc/logos" ] && [ "$(ls -A target/doc/logos)" ]; then
    print_status "Logo assets verified in documentation output"
    echo "Available logo files:"
    ls -la target/doc/logos/
else
    print_warning "Logo assets may not have been copied correctly"
fi

# Display completion message
print_status "Documentation build complete!"
echo ""
echo "Documentation available at: target/doc/index.html"
echo "Logo assets available at: target/doc/logos/"
echo ""
echo "To view the documentation, run:"
echo "  cargo doc --open"
echo "or open target/doc/index.html in your browser"
