#!/bin/bash
# Show directory structure for a site
# Usage: tools/check/tree-view.sh <site-dir>
# Example: tools/check/tree-view.sh sites/example.com-v1
# Exit codes: 0=success, 2=input error

set -e

SITE_DIR="${1:-.}"

if [[ ! -d "$SITE_DIR" ]]; then
    echo "ERROR:directory_not_found:$SITE_DIR"
    exit 2
fi

echo "=== Directory Structure: $(basename "$SITE_DIR") ==="
echo ""

# Try tree command first, fallback to find
if command -v tree &> /dev/null; then
    tree -L 2 --noreport "$SITE_DIR" | head -50
else
    # Fallback using find
    echo "$SITE_DIR/"
    find "$SITE_DIR" -maxdepth 2 -type f -o -type d 2>/dev/null | \
        sort | \
        while read -r path; do
            rel="${path#$SITE_DIR/}"
            if [[ -d "$path" ]]; then
                echo "├── $rel/"
            else
                echo "├── $rel"
            fi
        done | head -50
fi

echo ""

# Show file sizes
echo "=== File Sizes ==="
find "$SITE_DIR" -maxdepth 2 -type f \( -name "*.html" -o -name "*.css" -o -name "*.js" \) 2>/dev/null | \
    while read -r f; do
        size=$(wc -c < "$f" | tr -d ' ')
        kb=$(echo "scale=1; $size / 1024" | bc)
        printf "  %-25s %6.1f KB\n" "$(basename "$f")" "$kb"
    done
