#!/usr/bin/env nu

# Build Documentation Script
# Nushell version of build-docs.sh
# Generates cargo documentation with logo assets for RUSTELO

def main [] {
    print $"(ansi blue)📚 Building RUSTELO documentation with logo assets...(ansi reset)"

    # Verify project structure - following configuration-driven approach
    validate_project_structure

    # Clean previous documentation build
    print $"(ansi blue)🧹 Cleaning previous documentation...(ansi reset)"
    try {
        cargo clean --doc
    } catch {
        print $"(ansi yellow)⚠️ Failed to clean documentation, continuing...(ansi reset)"
    }

    # Build documentation
    print $"(ansi blue)📖 Generating cargo documentation...(ansi reset)"
    try {
        cargo doc --no-deps --lib --workspace --document-private-items
        print $"(ansi green)✅ Documentation generated successfully(ansi reset)"
    } catch {
        print $"(ansi red)❌ Failed to generate documentation(ansi reset)"
        exit 1
    }

    # Copy logo assets
    copy_logo_assets

    # Display completion information
    show_completion_info
}

# Validate project structure before proceeding
def validate_project_structure [] {
    # Check for Cargo.toml
    if not ("Cargo.toml" | path exists) {
        print $"(ansi red)❌ Cargo.toml not found. Please run this script from the project root directory.(ansi reset)"
        exit 1
    }

    # Check for logos directory
    if not ("logos" | path exists) {
        print $"(ansi red)❌ logos directory not found. Please ensure the logos directory exists in the project root.(ansi reset)"
        exit 1
    }

    print $"(ansi green)✅ Project structure validated(ansi reset)"
}

# Copy logo assets to documentation output
def copy_logo_assets [] {
    print $"(ansi blue)🖼️ Copying logo assets to documentation output...(ansi reset)"

    # Check if documentation output exists
    if not ("target/doc" | path exists) {
        print $"(ansi red)❌ Documentation output directory not found(ansi reset)"
        exit 1
    }

    # Copy logos directory
    try {
        cp -r logos target/doc/
        print $"(ansi green)✅ Logo assets copied to target/doc/logos/(ansi reset)"
    } catch {
        print $"(ansi red)❌ Failed to copy logo assets(ansi reset)"
        exit 1
    }

    # Verify logos were copied successfully
    verify_logo_assets
}

# Verify logo assets were copied correctly
def verify_logo_assets [] {
    let logos_dir = "target/doc/logos"

    if ($logos_dir | path exists) {
        let logo_files = (ls $logos_dir)

        if not ($logo_files | is-empty) {
            print $"(ansi green)✅ Logo assets verified in documentation output(ansi reset)"
            print $"(ansi blue)📁 Available logo files:(ansi reset)"

            # Display logo files with better formatting
            $logo_files | each {|file|
                let size = $file.size
                print $"  • ($file.name) - ($size)"
            }
        } else {
            print $"(ansi yellow)⚠️ Logos directory exists but appears empty(ansi reset)"
        }
    } else {
        print $"(ansi yellow)⚠️ Logo assets may not have been copied correctly(ansi reset)"
    }
}

# Show completion information and next steps
def show_completion_info [] {
    print $"(ansi green)✅ Documentation build complete!(ansi reset)"
    print ""
    print $"(ansi blue)📄 Documentation available at: target/doc/index.html(ansi reset)"
    print $"(ansi blue)🖼️ Logo assets available at: target/doc/logos/(ansi reset)"
    print ""
    print $"(ansi yellow)💡 To view the documentation, run:(ansi reset)"
    print $"  cargo doc --open"
    print ""

    # Show documentation size info
    if ("target/doc" | path exists) {
        let doc_size = (du target/doc | get apparent | first)
        print $"(ansi blue)📊 Documentation size: ($doc_size)(ansi reset)"
    }
}
