#!/bin/sh
# ============================================================
#  完整同步：昨日（09/06-09/07）全部改動 + 今日會計收支表
#  包含：工人欄位重組、申請批文進度、勞工申請進度、
#        租金按宿舍（含 DormRentIncome）、代理公司欄、
#        收租月曆、會計收支表新頁面
#  適用：154.94.239.207 (BT Panel)
#  請以 root 執行（寶塔面板「終端機」內貼上）
#  前置：wkr_full_sync.tar.gz 已上傳到 /home/wing/wkr_tmp/
# ============================================================
set -e

PROJ=/www/wwwroot/faithfulness
BASE=/home/wing/wkr_tmp

echo "=== [1/5] 備份資料庫 ==="
export MYSQL_PWD=$(grep '^DB_PASSWORD=' $PROJ/.env | cut -d= -f2)
DB_USER=$(grep '^DB_USERNAME=' $PROJ/.env | cut -d= -f2)
DB_NAME=$(grep '^DB_DATABASE=' $PROJ/.env | cut -d= -f2)
mkdir -p $BASE/backup
/www/server/mysql/bin/mysqldump -h 127.0.0.1 -u "$DB_USER" --single-transaction "$DB_NAME" > $BASE/backup/db_before_fullsync_$(date +%Y%m%d_%H%M).sql 2>/dev/null || true
ls -la $BASE/backup/db_before_fullsync_* 2>/dev/null | tail -1
echo "備份完成"

echo "=== [2/5] 解壓 ==="
rm -rf $BASE/extract_full
mkdir -p $BASE/extract_full
tar -xzf $BASE/wkr_full_sync.tar.gz -C $BASE/extract_full
echo "檔案數: $(find $BASE/extract_full -type f | wc -l)"

echo "=== [3/5] 複製到專案 + 權限 ==="
cd $BASE/extract_full
find . -type f | while read -r f; do
  mkdir -p "$PROJ/$(dirname "$f")"
  cp -a "$f" "$PROJ/$f"
done
chown -R www:www $PROJ/app $PROJ/database $PROJ/routes $PROJ/resources $PROJ/public/css
echo "完成"

echo "=== [4/5] migration + 清快取 ==="
cd $PROJ
sudo -u www /usr/bin/php artisan migrate --force
sudo -u www /usr/bin/php artisan optimize:clear 2>&1 | tail -1

echo "=== [5/5] 驗證 ==="
sudo -u www /usr/bin/php artisan migrate:status 2>/dev/null | tail -3
sudo -u www /usr/bin/php artisan tinker --execute="echo 'accounting_transactions: '.(Illuminate\Support\Facades\Schema::hasTable('accounting_transactions')?'OK':'MISSING').PHP_EOL; echo 'dorm_rent_incomes: '.(Illuminate\Support\Facades\Schema::hasTable('dorm_rent_incomes')?'OK':'MISSING').PHP_EOL; echo 'labor_application_steps: '.(Illuminate\Support\Facades\Schema::hasTable('labor_application_steps')?'OK':'MISSING').PHP_EOL; echo 'application_progresses: '.(Illuminate\Support\Facades\Schema::hasTable('application_progresses')?'OK':'MISSING').PHP_EOL; echo 'workers.mainland_id: '.(Illuminate\Support\Facades\Schema::hasColumn('workers','mainland_id')?'OK':'MISSING').PHP_EOL; echo 'dormitories.agency_company: '.(Illuminate\Support\Facades\Schema::hasColumn('dormitories','agency_company')?'OK':'MISSING').PHP_EOL;" 2>/dev/null | tail -6
echo "-- statement routes: $(sudo -u www /usr/bin/php artisan route:list 2>/dev/null | grep -c accounting.statement) (9 = OK)"
echo "-- labor_steps routes: $(sudo -u www /usr/bin/php artisan route:list 2>/dev/null | grep -c labor_application_steps) (6 = OK)"
echo "-- rent-receipts routes: $(sudo -u www /usr/bin/php artisan route:list 2>/dev/null | grep -c rent-receipts) (2 = OK)"
echo "-- sidebar 收租管理: $(grep -c '收租管理' resources/views/layouts/admin.blade.php) (>=1 = OK)"
echo "-- sidebar 會計收支表: $(grep -c '會計收支表' resources/views/layouts/admin.blade.php) (>=1 = OK)"
echo "=== 全部完成，Ctrl+F5 重新整理頁面 ==="
