#!/usr/bin/env nu

# Leptos Build Script
# Nushell version of leptos-build.sh
# Full build pipeline for Leptos application with dependencies and CSS

def main [] {
    print $"(ansi blue)🏗️  Leptos Production Build Pipeline(ansi reset)"

    # Install main dependencies
    install_main_dependencies

    # Install end2end dependencies
    install_e2e_dependencies

    # Build CSS assets
    build_css_assets

    # Build Leptos application
    build_leptos_application

    print $"(ansi green)🎉 Leptos build pipeline completed successfully!(ansi reset)"
}

# Install main project dependencies
def install_main_dependencies [] {
    print $"(ansi blue)📦 Installing main project dependencies...(ansi reset)"

    try {
        pnpm i
        print $"(ansi green)✅ Main dependencies installed(ansi reset)"
    } catch {
        print $"(ansi red)❌ Failed to install main dependencies(ansi reset)"
        exit 1
    }
}

# Install end-to-end test dependencies
def install_e2e_dependencies [] {
    print $"(ansi blue)📦 Installing end-to-end test dependencies...(ansi reset)"

    if ("end2end" | path exists) {
        try {
            cd end2end
            pnpm i
            cd ..
            print $"(ansi green)✅ End-to-end dependencies installed(ansi reset)"
        } catch {
            print $"(ansi red)❌ Failed to install end-to-end dependencies(ansi reset)"
            cd ..
            exit 1
        }
    } else {
        print $"(ansi yellow)⚠️  end2end directory not found, skipping e2e dependencies(ansi reset)"
    }
}

# Build CSS assets
def build_css_assets [] {
    print $"(ansi blue)🎨 Building CSS assets...(ansi reset)"

    try {
        pnpm build:css
        print $"(ansi green)✅ CSS assets built successfully(ansi reset)"
    } catch {
        print $"(ansi red)❌ Failed to build CSS assets(ansi reset)"
        exit 1
    }
}

# Build Leptos application with production settings
def build_leptos_application [] {
    print $"(ansi blue)🚀 Building Leptos application for production...(ansi reset)"
    print $"(ansi blue)📋 Features: tls, content-static(ansi reset)"
    print $"(ansi blue)⚡ Optimizations: release mode, JS minification(ansi reset)"

    try {
        cargo leptos build -r --js-minify true --features "tls,content-static"
        print $"(ansi green)✅ Leptos application built successfully(ansi reset)"

        # Show build results
        show_build_results
    } catch {
        print $"(ansi red)❌ Failed to build Leptos application(ansi reset)"
        exit 1
    }
}

# Show build results and statistics
def show_build_results [] {
    print $"(ansi blue)📊 Build Results:(ansi reset)"

    # Check for server binary
    if ("target/release/server" | path exists) {
        let server_size = (du "target/release/server" | get apparent | first)
        print $"(ansi green)  🖥️  Server binary: ($server_size)(ansi reset)"
    }

    # Check for site directory
    if ("target/site" | path exists) {
        let site_files = (ls target/site | length)
        let site_size = (du target/site | get apparent | first)
        print $"(ansi green)  🌐 Site assets: ($site_files) files, ($site_size)(ansi reset)"

        # Show key site files
        let js_files = (ls target/site/**/*.js | length)
        let wasm_files = (ls target/site/**/*.wasm | length)
        let css_files = (ls target/site/**/*.css | length)

        print $"(ansi blue)    📄 JavaScript files: ($js_files)(ansi reset)"
        print $"(ansi blue)    🦀 WebAssembly files: ($wasm_files)(ansi reset)"
        print $"(ansi blue)    🎨 CSS files: ($css_files)(ansi reset)"
    }

    # Check for public directory
    if ("site/public" | path exists) {
        let public_size = (du site/public | get apparent | first)
        print $"(ansi green)  📁 Public assets: ($public_size)(ansi reset)"
    }

    print ""
    print $"(ansi blue)🚀 Ready for deployment!(ansi reset)"
    print $"(ansi yellow)💡 Next steps:(ansi reset)"
    print "  1. Test the build: cargo run --release --features tls,content-static"
    print "  2. Deploy using: ./scripts/build/deploy.nu deploy"
    print "  3. Or create distribution package: ./scripts/build/dist-pack.nu"
}
