#!/usr/bin/env nu

# Logo & Image Deployment Script
#
# Copies logo and image files from source directories to public/ for deployment.
# Recursively copies subdirectories (e.g. logos/projects/, images/projects/).
#
# Source directories:
# - site/assets/logos/        — site-specific logos
# - rustelo/assets/logos/     — framework logos
# - site/assets/images/       — site-specific images (if exists)
#
# Destination:
# - site/public/logos/        — served to clients
# - site/public/images/       — served to clients
#
# Usage:
#   nu scripts/build/copy-logos.nu

# Recursively copy a source directory into a destination directory,
# preserving subdirectory structure.
def copy_dir_recursive [src_dir: string, dst_dir: string]: nothing -> list<string> {
    mut copied = []

    if not ($dst_dir | path exists) {
        mkdir $dst_dir
    }

    let entries = (ls $src_dir -a | where { |e| ($e.name | path basename) != ".DS_Store" })

    for entry in $entries {
        let name = ($entry.name | path basename)
        let src_path = $entry.name
        let dst_path = $"($dst_dir)/($name)"

        if $entry.type == "dir" {
            let sub_copied = (copy_dir_recursive $src_path $dst_path)
            $copied = ($copied | append $sub_copied)
        } else {
            cp $src_path $dst_path
            let rel = ($src_path | str replace $src_dir "" | str trim --left --char '/')
            $copied = ($copied | append $rel)
        }
    }

    $copied
}

def main [] {
    print $"(ansi blue)Logo & Image Deployment(ansi reset)"

    let logo_sources = [
        "site/assets/logos"
        "rustelo/assets/logos"
    ]

    let image_sources = [
        "site/assets/images"
    ]

    let public_logos_dir = "site/public/images/logos"
    let public_images_dir = "site/public/images"

    mut copied_files = []
    mut missing_dirs = []

    # --- Logos ---
    for source_dir in $logo_sources {
        if not ($source_dir | path exists) {
            $missing_dirs = ($missing_dirs | append $source_dir)
            continue
        }

        let result = (copy_dir_recursive $source_dir $public_logos_dir)
        $copied_files = ($copied_files | append ($result | each { |f| $"logos/($f)" }))
    }

    # --- Images ---
    for source_dir in $image_sources {
        if not ($source_dir | path exists) {
            $missing_dirs = ($missing_dirs | append $source_dir)
            continue
        }

        let result = (copy_dir_recursive $source_dir $public_images_dir)
        $copied_files = ($copied_files | append ($result | each { |f| $"images/($f)" }))
    }

    print $"(ansi green)Logo & Image Deployment Complete!(ansi reset)"
    print ""

    if ($copied_files | length) > 0 {
        print $"(ansi green)Copied:(ansi reset)"
        for file in $copied_files {
            print $"   ($file)"
        }
    }

    if ($missing_dirs | length) > 0 {
        print ""
        print $"(ansi yellow)Missing source directories [skipped]:(ansi reset)"
        for dir in $missing_dirs {
            print $"   ($dir)"
        }
    }

    let total_files = ($copied_files | length)

    print ""
    print $"(ansi cyan)Summary: ($total_files) files copied(ansi reset)"
    print $"(ansi green)Ready for serving from site/public/(ansi reset)"
}
