#!/bin/bash
# Export Review Archive Script
#
# Purpose: Generate a clean tarball for external review, excluding build artifacts,
#          caches, and unnecessary files.
#
# Usage: ./scripts/export-review-archive.sh [output-name]
#        Default output: daily-notification-plugin-review-YYYYMMDD.tar.gz
#
# Author: Matthew Raymer
# Date: 2025-12-23

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"

# Generate output filename
if [ $# -ge 1 ]; then
    OUTPUT_NAME="$1"
else
    OUTPUT_NAME="daily-notification-plugin-review-$(date +%Y%m%d).tar.gz"
fi

# Ensure output is in parent directory (not inside project)
OUTPUT_PATH="$(cd "$PROJECT_ROOT/.." && pwd)/$OUTPUT_NAME"

echo "📦 Creating review archive: $OUTPUT_NAME"
echo "   Output: $OUTPUT_PATH"
echo ""

# Create archive excluding build artifacts, caches, and unnecessary files
tar -czf "$OUTPUT_PATH" \
    --exclude='.git' \
    --exclude='node_modules' \
    --exclude='dist' \
    --exclude='build' \
    --exclude='.gradle' \
    --exclude='android/.gradle' \
    --exclude='android/build' \
    --exclude='android/app/build' \
    --exclude='ios/Pods' \
    --exclude='ios/DerivedData' \
    --exclude='ios/*.xcworkspace/xcuserdata' \
    --exclude='ios/*.xcodeproj/xcuserdata' \
    --exclude='*.tar.gz' \
    --exclude='*.zip' \
    --exclude='.DS_Store' \
    --exclude='*.swp' \
    --exclude='*.swo' \
    --exclude='*~' \
    --exclude='.idea' \
    --exclude='.vscode' \
    --exclude='packages/*/dist' \
    --exclude='packages/*/build' \
    --exclude='packages/*/node_modules' \
    --exclude='coverage' \
    --exclude='.nyc_output' \
    --exclude='*.log' \
    -C "$(dirname "$PROJECT_ROOT")" \
    "$(basename "$PROJECT_ROOT")"

# Verify archive was created
if [ -f "$OUTPUT_PATH" ]; then
    SIZE=$(du -h "$OUTPUT_PATH" | cut -f1)
    echo "✅ Archive created successfully"
    echo "   Size: $SIZE"
    echo "   Path: $OUTPUT_PATH"
    echo ""
    echo "📋 Archive contents preview:"
    tar -tzf "$OUTPUT_PATH" | head -20
    echo "   ... (showing first 20 entries)"
    echo ""
    echo "To extract: tar -xzf $OUTPUT_NAME"
else
    echo "❌ Failed to create archive"
    exit 1
fi

