PROJECT_NAME=$(basename "$PWD")
TIMESTAMP=$(date +"%d-%m-%y_%H.%M")
OUTPUT_FILE="./report/${PROJECT_NAME}_${TIMESTAMP}.txt"

mkdir -p ./report && {
  echo "=== FLUTTER PROJECT BACKUP ==="
  echo ""
  echo "=== PROJECT STRUCTURE (LIB, ASSETS & PUBLIC) ==="
  find lib assets public -type f 2>/dev/null | sort
  echo ""
  echo "=== pubspec.yaml ==="
  cat pubspec.yaml 2>/dev/null
  echo ""
  echo "=== MAC OS CONFIG ==="
  echo "--- Info.plist ---"
  cat macos/Runner/Info.plist 2>/dev/null
  echo "--- Entitlements ---"
  cat macos/Runner/*.entitlements 2>/dev/null
  echo "--- Podfile ---"
  cat macos/Podfile 2>/dev/null
  echo ""
  echo "=== IOS CONFIG ==="
  echo "--- Info.plist ---"
  cat ios/Runner/Info.plist 2>/dev/null
  echo "--- Podfile ---"
  cat ios/Podfile 2>/dev/null
  echo ""
  echo "=== ANDROID CONFIG ==="
  echo "--- AndroidManifest.xml ---"
  cat android/app/src/main/AndroidManifest.xml 2>/dev/null
  echo "--- build.gradle / build.gradle.kts ---"
  cat android/app/build.gradle 2>/dev/null
  cat android/app/build.gradle.kts 2>/dev/null
  echo ""
  echo "=== WEB / FIREBASE (public/) ==="
  find public -type f \( -name "*.html" -o -name "*.js" -o -name "*.css" -o -name "*.json" \) 2>/dev/null | sort | while read -r file; do
    echo ""
    echo "// ==========================================================================="
    echo "// FILE: $file"
    echo "// ==========================================================================="
    echo ""
    cat "$file"
  done
  echo ""
  echo "=== SOURCE CODE (lib/) ==="
  find lib -type f -name "*.dart" 2>/dev/null | sort | while read -r file; do
    echo ""
    echo "// ==========================================================================="
    echo "// FILE: $file"
    echo "// ==========================================================================="
    echo ""
    cat "$file"
  done
} > "$OUTPUT_FILE" && echo "Backup completato! Artefatto salvato in: $OUTPUT_FILE"