#!/usr/bin/env nu

# Documentation postprocessing
#
# Handles index creation, format conversion, and document counting

use std log

# Create documentation index
export def create_documentation_index [docs_config: record, generation_results: list] {
    log info "Creating documentation index..."

    let start_time = (date now)

    let result = (do {
        let index_content = $"# Documentation Index

Welcome to the Provisioning System documentation!

## User Documentation
- [Getting Started Guide](user/getting-started.md) - Quick start guide for new users
- [Installation Guide](user/installation.md) - Complete installation instructions
- [CLI Reference](user/cli-reference.md) - Comprehensive command reference
- [Troubleshooting Guide](user/troubleshooting.md) - Common issues and solutions
- [FAQ](user/faq.md) - Frequently asked questions

## Administrator Documentation
- Configuration Guide - Advanced configuration options
- Security Guide - Security best practices and configuration
- Monitoring Guide - System monitoring and observability
- Backup and Recovery - Data protection strategies

## Developer Documentation
- Architecture Overview - System architecture and design
- API Reference - REST API documentation
- Plugin Development - Creating custom plugins and extensions
- Contributing Guide - How to contribute to the project

## Generated Documentation
This documentation was generated automatically on (date now | format date '%Y-%m-%d %H:%M:%S').

For the most up-to-date information, visit the online documentation.
"

        $index_content | save ($docs_config.output_dir | path join "README.md")

        {
            status: "success"
            index_file: ($docs_config.output_dir | path join "README.md")
            duration: ((date now) - $start_time)
        }
    } | complete)

    if $result.exit_code != 0 {
        {
            status: "failed"
            reason: $result.stderr
            duration: ((date now) - $start_time)
        }
    } else {
        $result.stdout
    }
}

# Convert documentation to additional formats (placeholder)
export def convert_documentation_formats [docs_config: record, generation_results: list] {
    log info "Converting documentation formats..."

    let start_time = (date now)

    # Format conversion would happen here (markdown to HTML, PDF, etc.)
    log warning "Documentation format conversion not fully implemented"

    {
        status: "skipped"
        reason: "format conversion not fully implemented"
        converted_files: 0
        duration: ((date now) - $start_time)
    }
}

# Count generated documents from results
export def count_generated_documents [generation_results: list] {
    let user_result = (do {
        let user_result = ($generation_results | where phase == "user" | get 0.result)
        $user_result.docs_generated
    } | complete)
    let user_count = if $user_result.exit_code == 0 { $user_result.stdout } else { 0 }

    let admin_result = (do {
        let admin_result = ($generation_results | where phase == "admin" | get 0.result)
        $admin_result.docs_generated
    } | complete)
    let admin_count = if $admin_result.exit_code == 0 { $admin_result.stdout } else { 0 }

    let dev_result = (do {
        let dev_result = ($generation_results | where phase == "dev" | get 0.result)
        $dev_result.docs_generated
    } | complete)
    let dev_count = if $dev_result.exit_code == 0 { $dev_result.stdout } else { 0 }

    let api_result = (do {
        let api_result = ($generation_results | where phase == "api" | get 0.result)
        $api_result.docs_generated
    } | complete)
    let api_count = if $api_result.exit_code == 0 { $api_result.stdout } else { 0 }

    return ($user_count + $admin_count + $dev_count + $api_count + 1) # +1 for index
}

# Get directory size helper
export def get_directory_size [dir: string] {
    if not ($dir | path exists) {
        return 0
    }

    let result = (do {
        ls $dir | where type == file | each {|file| $file.size } | math sum
    } | complete)

    if $result.exit_code == 0 {
        $result.stdout
    } else {
        0
    }
}
