#!/bin/bash
#
# pull-tailwind-docs.sh
# Pulls latest Tailwind CSS documentation → combined markdown for Claude Projects
#
# Usage: ./pull-tailwind-docs.sh [output_dir]
#        Default: ./tailwind-docs-output
#
# Requirements: git
#

set -e

OUTPUT_DIR="${1:-./tailwind-docs-output}"
REPO_URL="https://github.com/tailwindlabs/tailwindcss.com.git"
TEMP_DIR=$(mktemp -d)
DATE=$(date +%Y-%m-%d)

cleanup() { rm -rf "$TEMP_DIR"; }
trap cleanup EXIT

echo "=== Tailwind CSS Docs Puller ==="
echo "Output: $OUTPUT_DIR"
echo ""

mkdir -p "$OUTPUT_DIR"

# Full shallow clone (repo is small enough, ~50MB)
echo "Cloning tailwindcss.com repo (shallow)..."
git clone --depth 1 "$REPO_URL" "$TEMP_DIR/docs" 2>&1 | grep -v "^remote:" || true
cd "$TEMP_DIR/docs"
COMMIT_SHA=$(git rev-parse --short HEAD)
cd - > /dev/null

# Find where the MDX docs actually live
echo "Searching for documentation files..."
DOCS_DIR=""

# Try common paths
for try_path in \
    "$TEMP_DIR/docs/src/pages/docs" \
    "$TEMP_DIR/docs/src/app/docs" \
    "$TEMP_DIR/docs/src/docs" \
    "$TEMP_DIR/docs/src/content/docs" \
    "$TEMP_DIR/docs/docs" \
    "$TEMP_DIR/docs/content/docs"; do
    if [ -d "$try_path" ]; then
        MDX_COUNT=$(find "$try_path" -name "*.mdx" 2>/dev/null | wc -l)
        if [ "$MDX_COUNT" -gt 10 ]; then
            DOCS_DIR="$try_path"
            echo "Found docs at: ${try_path#$TEMP_DIR/docs/} ($MDX_COUNT MDX files)"
            break
        fi
    fi
done

# If not found, search for highest concentration of MDX files
if [ -z "$DOCS_DIR" ]; then
    echo "Standard paths not found, searching for MDX files..."
    
    # Find directory with most MDX files
    BEST_DIR=$(find "$TEMP_DIR/docs/src" -type f -name "*.mdx" 2>/dev/null | \
        xargs -I{} dirname {} | sort | uniq -c | sort -rn | head -1 | awk '{print $2}')
    
    if [ -n "$BEST_DIR" ] && [ -d "$BEST_DIR" ]; then
        # Go up one level if we're in a subdirectory
        PARENT_DIR=$(dirname "$BEST_DIR")
        PARENT_COUNT=$(find "$PARENT_DIR" -name "*.mdx" 2>/dev/null | wc -l)
        BEST_COUNT=$(find "$BEST_DIR" -name "*.mdx" 2>/dev/null | wc -l)
        
        if [ "$PARENT_COUNT" -gt "$BEST_COUNT" ]; then
            DOCS_DIR="$PARENT_DIR"
        else
            DOCS_DIR="$BEST_DIR"
        fi
        
        MDX_COUNT=$(find "$DOCS_DIR" -name "*.mdx" | wc -l)
        echo "Found docs at: ${DOCS_DIR#$TEMP_DIR/docs/} ($MDX_COUNT MDX files)"
    fi
fi

# Still not found? Show what we have
if [ -z "$DOCS_DIR" ] || [ ! -d "$DOCS_DIR" ]; then
    echo ""
    echo "ERROR: Could not locate documentation MDX files."
    echo ""
    echo "Repository structure:"
    find "$TEMP_DIR/docs" -maxdepth 3 -type d | head -30
    echo ""
    echo "MDX files found:"
    find "$TEMP_DIR/docs" -name "*.mdx" | head -20
    exit 1
fi

OUTPUT_FILE="$OUTPUT_DIR/tailwind-docs-combined.md"

# Header
cat > "$OUTPUT_FILE" << EOF
# Tailwind CSS Documentation

- **Source:** https://github.com/tailwindlabs/tailwindcss.com
- **Commit:** $COMMIT_SHA
- **Pulled:** $DATE
- **Live:** https://tailwindcss.com/docs

---
EOF

# Process all MDX/MD files
FILE_COUNT=$(find "$DOCS_DIR" -type f \( -name "*.mdx" -o -name "*.md" \) | wc -l)
echo "Processing $FILE_COUNT files..."

find "$DOCS_DIR" -type f \( -name "*.mdx" -o -name "*.md" \) | sort | while read -r file; do
    REL_PATH="${file#$DOCS_DIR/}"
    REL_PATH="${REL_PATH%.mdx}"
    REL_PATH="${REL_PATH%.md}"
    
    {
        echo ""
        echo "---"
        echo ""
        echo "## 📄 $REL_PATH"
        echo ""
        cat "$file"
        echo ""
    } >> "$OUTPUT_FILE"
done

SIZE_KB=$(du -k "$OUTPUT_FILE" | cut -f1)
SIZE_H=$(du -h "$OUTPUT_FILE" | cut -f1)
LINES=$(wc -l < "$OUTPUT_FILE")

echo ""
echo "=== Complete ==="
echo "File: $OUTPUT_FILE"
echo "Size: $SIZE_H ($LINES lines)"
echo ""
echo "✅ Upload to Claude Project Files!"
