#!/usr/bin/env bash
set -euo pipefail

PROJECT="/home/ubuntu/clinic-management"
STAGE="/home/ubuntu/clinic-management-delivery"
OUT="/home/ubuntu/clinic-management-delivery.zip"
rm -rf "$STAGE" "$OUT"
mkdir -p "$STAGE/project" "$STAGE/additional-files/presentation_users_permissions_performance" "$STAGE/additional-files/source-documents"

# Project source and deployable assets; runtime dependencies, secrets, caches and logs are excluded.
for item in artisan app bootstrap config database lang public resources routes storage tests composer.json composer.lock package.json pnpm-lock.yaml vite.config.js postcss.config.js tailwind.config.js phpunit.xml README.md scripts; do
  if [ -e "$PROJECT/$item" ]; then cp -a "$PROJECT/$item" "$STAGE/project/"; fi
done
rm -rf "$STAGE/project/storage/framework" "$STAGE/project/storage/logs" "$STAGE/project/storage/app/public"
find "$STAGE/project" -type f \( -name '.env' -o -name '.env.*' \) -delete

# Include the presentation project created during the work, if present.
if [ -d "$PROJECT/presentation_users_permissions_performance" ]; then
  cp -a "$PROJECT/presentation_users_permissions_performance/." "$STAGE/additional-files/presentation_users_permissions_performance/"
fi
# Include the original uploaded source document, if present.
if [ -f "/home/ubuntu/upload/تطبيقادارةالعيادات.docx" ]; then
  cp -a "/home/ubuntu/upload/تطبيقادارةالعيادات.docx" "$STAGE/additional-files/source-documents/"
fi

cat > "$STAGE/README_DELIVERY.md" <<'EOF'
# Clinic Management System — Delivery Package

This archive contains the Laravel project source and the additional presentation/source document created during the implementation work.

## Included

- Laravel application source: `project/`
- Database migrations, seeders, and the Egyptian medicines workbook: `project/database/`
- Blade templates, bilingual language files, routes, controllers, services, tests, and reusable scripts.
- Presentation project: `additional-files/presentation_users_permissions_performance/`
- Original uploaded requirements document, when available: `additional-files/source-documents/`

## Excluded intentionally

The archive does not include `.env` files, `vendor/`, `node_modules/`, runtime caches, generated logs, or private credentials. Create a local `.env` from `.env.example` and install dependencies before running the application.

## Basic setup

```bash
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate
php artisan db:seed --class=Database\\Seeders\\MedicalCatalogSeeder
npm install
npm run build
php artisan serve
```
EOF

(cd "$STAGE" && zip -qr "$OUT" .)

printf 'archive=%s\n' "$OUT"
printf 'size_bytes=%s\n' "$(stat -c%s "$OUT")"
printf 'files=%s\n' "$(unzip -Z1 "$OUT" | wc -l)"
