#!/bin/bash

# 后端模块重构脚本
# 批量复制和修改文件

set -e

BASE_DIR="d:/project/company/rent/apps/server/src/modules"

echo "🚀 开始后端模块重构..."

# ============================================
# 用户端模块迁移
# ============================================
echo ""
echo "📦 迁移用户端模块..."

# 1. user/profile (从 user/user-user.controller.ts)
echo "  - 迁移 user/profile..."
cp "$BASE_DIR/user/user.service.ts" "$BASE_DIR/user/profile/"
cp "$BASE_DIR/user/user-user.controller.ts" "$BASE_DIR/user/profile/profile.controller.ts"
cp -r "$BASE_DIR/user/dto" "$BASE_DIR/user/profile/"

# 修改 profile.controller.ts 的路由前缀
sed -i "s/@Controller('user')/@Controller('api\/user\/profile')/g" "$BASE_DIR/user/profile/profile.controller.ts"
sed -i "s/UserUserController/ProfileController/g" "$BASE_DIR/user/profile/profile.controller.ts"
sed -i "s/UserService/ProfileService/g" "$BASE_DIR/user/profile/profile.controller.ts"

# 重命名 service
mv "$BASE_DIR/user/profile/user.service.ts" "$BASE_DIR/user/profile/profile.service.ts"
sed -i "s/export class UserService/export class ProfileService/g" "$BASE_DIR/user/profile/profile.service.ts"

# 2. user/guest
echo "  - 迁移 user/guest..."
cp "$BASE_DIR/guest/guest.service.ts" "$BASE_DIR/user/guest/"
cp "$BASE_DIR/guest/guest.controller.ts" "$BASE_DIR/user/guest/"
cp -r "$BASE_DIR/guest/dto" "$BASE_DIR/user/guest/"

sed -i "s/@Controller('user\/guests')/@Controller('api\/user\/guests')/g" "$BASE_DIR/user/guest/guest.controller.ts"

# 3. user/order
echo "  - 迁移 user/order..."
cp "$BASE_DIR/order/order-user.controller.ts" "$BASE_DIR/user/order/order.controller.ts"

sed -i "s/@Controller('orders')/@Controller('api\/user\/orders')/g" "$BASE_DIR/user/order/order.controller.ts"
sed -i "s/OrderUserController/OrderController/g" "$BASE_DIR/user/order/order.controller.ts"

# 4. user/review
echo "  - 迁移 user/review..."
cp "$BASE_DIR/review/review-user.controller.ts" "$BASE_DIR/user/review/review.controller.ts"

sed -i "s/@Controller('reviews')/@Controller('api\/user\/reviews')/g" "$BASE_DIR/user/review/review.controller.ts"
sed -i "s/ReviewUserController/ReviewController/g" "$BASE_DIR/user/review/review.controller.ts"

# 5. user/coupon
echo "  - 迁移 user/coupon..."
cp "$BASE_DIR/coupon/coupon-user.controller.ts" "$BASE_DIR/user/coupon/coupon.controller.ts"

sed -i "s/@Controller('user\/coupons')/@Controller('api\/user\/coupons')/g" "$BASE_DIR/user/coupon/coupon.controller.ts"
sed -i "s/CouponUserController/CouponController/g" "$BASE_DIR/user/coupon/coupon.controller.ts"

# 6. user/finance
echo "  - 迁移 user/finance..."
cp "$BASE_DIR/finance/finance-user.controller.ts" "$BASE_DIR/user/finance/finance.controller.ts"
cp "$BASE_DIR/finance/withdrawal-user.controller.ts" "$BASE_DIR/user/finance/withdrawal.controller.ts"

sed -i "s/@Controller('user\/finance')/@Controller('api\/user\/finance')/g" "$BASE_DIR/user/finance/finance.controller.ts"
sed -i "s/@Controller('user\/finance\/withdrawals')/@Controller('api\/user\/finance\/withdrawals')/g" "$BASE_DIR/user/finance/withdrawal.controller.ts"
sed -i "s/FinanceUserController/FinanceController/g" "$BASE_DIR/user/finance/finance.controller.ts"
sed -i "s/WithdrawalUserController/WithdrawalController/g" "$BASE_DIR/user/finance/withdrawal.controller.ts"

# 7. user/activity
echo "  - 迁移 user/activity..."
cp "$BASE_DIR/activity/activity-user.controller.ts" "$BASE_DIR/user/activity/activity.controller.ts"

sed -i "s/@Controller('user\/activity\/invite')/@Controller('api\/user\/activity\/invite')/g" "$BASE_DIR/user/activity/activity.controller.ts"
sed -i "s/ActivityUserController/ActivityController/g" "$BASE_DIR/user/activity/activity.controller.ts"

# ============================================
# 商家端模块迁移
# ============================================
echo ""
echo "📦 迁移商家端模块..."

# 1. merchant/auth
echo "  - 迁移 merchant/auth..."
cp "$BASE_DIR/seller-auth/seller-auth.service.ts" "$BASE_DIR/merchant/auth/auth.service.ts"
cp "$BASE_DIR/seller-auth/seller-auth.controller.ts" "$BASE_DIR/merchant/auth/auth.controller.ts"
cp "$BASE_DIR/seller-auth/seller-auth.module.ts" "$BASE_DIR/merchant/auth/auth.module.ts"
cp -r "$BASE_DIR/seller-auth/dto" "$BASE_DIR/merchant/auth/"

sed -i "s/@Controller('seller\/auth')/@Controller('api\/merchant\/auth')/g" "$BASE_DIR/merchant/auth/auth.controller.ts"
sed -i "s/SellerAuthController/AuthController/g" "$BASE_DIR/merchant/auth/auth.controller.ts"
sed -i "s/SellerAuthService/AuthService/g" "$BASE_DIR/merchant/auth/auth.controller.ts"
sed -i "s/SellerAuthService/AuthService/g" "$BASE_DIR/merchant/auth/auth.service.ts"
sed -i "s/SellerAuthModule/MerchantAuthModule/g" "$BASE_DIR/merchant/auth/auth.module.ts"

# 2. merchant/profile
echo "  - 迁移 merchant/profile..."
cp "$BASE_DIR/merchant/merchant-seller.controller.ts" "$BASE_DIR/merchant/profile/profile.controller.ts"

sed -i "s/@Controller('seller\/merchant')/@Controller('api\/merchant\/profile')/g" "$BASE_DIR/merchant/profile/profile.controller.ts"
sed -i "s/MerchantSellerController/ProfileController/g" "$BASE_DIR/merchant/profile/profile.controller.ts"

# 3. merchant/statistics
echo "  - 迁移 merchant/statistics..."
cp "$BASE_DIR/merchant/statistics-seller.controller.ts" "$BASE_DIR/merchant/statistics/statistics.controller.ts"

sed -i "s/@Controller('seller\/statistics')/@Controller('api\/merchant\/statistics')/g" "$BASE_DIR/merchant/statistics/statistics.controller.ts"
sed -i "s/StatisticsSellerController/StatisticsController/g" "$BASE_DIR/merchant/statistics/statistics.controller.ts"

# 4. merchant/room
echo "  - 迁移 merchant/room..."
cp "$BASE_DIR/room/room-seller.controller.ts" "$BASE_DIR/merchant/room/room.controller.ts"

sed -i "s/@Controller('seller\/rooms')/@Controller('api\/merchant\/rooms')/g" "$BASE_DIR/merchant/room/room.controller.ts"
sed -i "s/RoomSellerController/RoomController/g" "$BASE_DIR/merchant/room/room.controller.ts"

# 5. merchant/room-calendar
echo "  - 迁移 merchant/room-calendar..."
cp "$BASE_DIR/room-calendar/room-calendar.controller.ts" "$BASE_DIR/merchant/room-calendar/"
cp "$BASE_DIR/room-calendar/room-calendar.service.ts" "$BASE_DIR/merchant/room-calendar/"
cp "$BASE_DIR/room-calendar/room-calendar.module.ts" "$BASE_DIR/merchant/room-calendar/"
cp -r "$BASE_DIR/room-calendar/dto" "$BASE_DIR/merchant/room-calendar/"

sed -i "s/@Controller('seller\/room-calendar')/@Controller('api\/merchant\/room-calendar')/g" "$BASE_DIR/merchant/room-calendar/room-calendar.controller.ts"

# 6. merchant/order
echo "  - 迁移 merchant/order..."
cp "$BASE_DIR/order/order-seller.controller.ts" "$BASE_DIR/merchant/order/order.controller.ts"

sed -i "s/@Controller('seller\/orders')/@Controller('api\/merchant\/orders')/g" "$BASE_DIR/merchant/order/order.controller.ts"
sed -i "s/OrderSellerController/OrderController/g" "$BASE_DIR/merchant/order/order.controller.ts"

# 7. merchant/review
echo "  - 迁移 merchant/review..."
cp "$BASE_DIR/review/review-seller.controller.ts" "$BASE_DIR/merchant/review/review.controller.ts"

sed -i "s/@Controller('seller\/reviews')/@Controller('api\/merchant\/reviews')/g" "$BASE_DIR/merchant/review/review.controller.ts"
sed -i "s/ReviewSellerController/ReviewController/g" "$BASE_DIR/merchant/review/review.controller.ts"

# 8. merchant/finance
echo "  - 迁移 merchant/finance..."
cp "$BASE_DIR/finance/transaction-seller.controller.ts" "$BASE_DIR/merchant/finance/transaction.controller.ts"
cp "$BASE_DIR/finance/withdrawal-merchant.controller.ts" "$BASE_DIR/merchant/finance/withdrawal.controller.ts"
cp "$BASE_DIR/finance/settlement-merchant.controller.ts" "$BASE_DIR/merchant/finance/settlement.controller.ts"

sed -i "s/@Controller('merchant\/finance\/transactions')/@Controller('api\/merchant\/finance\/transactions')/g" "$BASE_DIR/merchant/finance/transaction.controller.ts"
sed -i "s/@Controller('merchant\/finance\/withdrawals')/@Controller('api\/merchant\/finance\/withdrawals')/g" "$BASE_DIR/merchant/finance/withdrawal.controller.ts"
sed -i "s/@Controller('merchant\/finance\/settlements')/@Controller('api\/merchant\/finance\/settlements')/g" "$BASE_DIR/merchant/finance/settlement.controller.ts"

# ============================================
# 管理端模块迁移
# ============================================
echo ""
echo "📦 迁移管理端模块..."

# 1. admin/auth
echo "  - 迁移 admin/auth..."
cp "$BASE_DIR/admin-auth/admin-auth.service.ts" "$BASE_DIR/admin/auth/auth.service.ts"
cp "$BASE_DIR/admin-auth/admin-auth.controller.ts" "$BASE_DIR/admin/auth/auth.controller.ts"
cp "$BASE_DIR/admin-auth/admin-auth.module.ts" "$BASE_DIR/admin/auth/auth.module.ts"
cp -r "$BASE_DIR/admin-auth/dto" "$BASE_DIR/admin/auth/"

sed -i "s/@Controller('admin\/auth')/@Controller('api\/admin\/auth')/g" "$BASE_DIR/admin/auth/auth.controller.ts"
sed -i "s/AdminAuthController/AuthController/g" "$BASE_DIR/admin/auth/auth.controller.ts"
sed -i "s/AdminAuthService/AuthService/g" "$BASE_DIR/admin/auth/auth.controller.ts"
sed -i "s/AdminAuthService/AuthService/g" "$BASE_DIR/admin/auth/auth.service.ts"
sed -i "s/AdminAuthModule/AdminAuthModule/g" "$BASE_DIR/admin/auth/auth.module.ts"

# 2. admin/user
echo "  - 迁移 admin/user..."
cp "$BASE_DIR/user/user-admin.controller.ts" "$BASE_DIR/admin/user/user.controller.ts"

sed -i "s/@Controller('admin\/users')/@Controller('api\/admin\/users')/g" "$BASE_DIR/admin/user/user.controller.ts"
sed -i "s/UserAdminController/UserController/g" "$BASE_DIR/admin/user/user.controller.ts"

# 3. admin/merchant
echo "  - 迁移 admin/merchant..."
cp "$BASE_DIR/merchant/merchant-admin.controller.ts" "$BASE_DIR/admin/merchant/merchant.controller.ts"

sed -i "s/@Controller('admin\/merchants')/@Controller('api\/admin\/merchants')/g" "$BASE_DIR/admin/merchant/merchant.controller.ts"
sed -i "s/MerchantAdminController/MerchantController/g" "$BASE_DIR/admin/merchant/merchant.controller.ts"

# 4. admin/room
echo "  - 迁移 admin/room..."
cp "$BASE_DIR/room/room-admin.controller.ts" "$BASE_DIR/admin/room/room.controller.ts"

sed -i "s/@Controller('admin\/rooms')/@Controller('api\/admin\/rooms')/g" "$BASE_DIR/admin/room/room.controller.ts"
sed -i "s/RoomAdminController/RoomController/g" "$BASE_DIR/admin/room/room.controller.ts"

# 5. admin/order
echo "  - 迁移 admin/order..."
cp "$BASE_DIR/order/order-admin.controller.ts" "$BASE_DIR/admin/order/order.controller.ts"

sed -i "s/@Controller('admin\/orders')/@Controller('api\/admin\/orders')/g" "$BASE_DIR/admin/order/order.controller.ts"
sed -i "s/OrderAdminController/OrderController/g" "$BASE_DIR/admin/order/order.controller.ts"

# 6. admin/review
echo "  - 迁移 admin/review..."
cp "$BASE_DIR/review/review-admin.controller.ts" "$BASE_DIR/admin/review/review.controller.ts"

sed -i "s/@Controller('admin\/reviews')/@Controller('api\/admin\/reviews')/g" "$BASE_DIR/admin/review/review.controller.ts"
sed -i "s/ReviewAdminController/ReviewController/g" "$BASE_DIR/admin/review/review.controller.ts"

# 7. admin/coupon
echo "  - 迁移 admin/coupon..."
cp "$BASE_DIR/coupon/coupon-admin.controller.ts" "$BASE_DIR/admin/coupon/coupon.controller.ts"

sed -i "s/@Controller('admin\/coupons')/@Controller('api\/admin\/coupons')/g" "$BASE_DIR/admin/coupon/coupon.controller.ts"
sed -i "s/CouponAdminController/CouponController/g" "$BASE_DIR/admin/coupon/coupon.controller.ts"

# 8. admin/activity
echo "  - 迁移 admin/activity..."
cp "$BASE_DIR/activity/activity-admin.controller.ts" "$BASE_DIR/admin/activity/activity.controller.ts"

sed -i "s/@Controller('admin\/activity')/@Controller('api\/admin\/activity')/g" "$BASE_DIR/admin/activity/activity.controller.ts"
sed -i "s/ActivityAdminController/ActivityController/g" "$BASE_DIR/admin/activity/activity.controller.ts"

# 9. admin/config
echo "  - 迁移 admin/config..."
cp "$BASE_DIR/config/config.controller.ts" "$BASE_DIR/admin/config/"
cp "$BASE_DIR/config/config.service.ts" "$BASE_DIR/admin/config/"
cp "$BASE_DIR/config/config.module.ts" "$BASE_DIR/admin/config/"
cp -r "$BASE_DIR/config/dto" "$BASE_DIR/admin/config/"

sed -i "s/@Controller('admin\/config')/@Controller('api\/admin\/config')/g" "$BASE_DIR/admin/config/config.controller.ts"

# 10. admin/finance
echo "  - 迁移 admin/finance..."
cp "$BASE_DIR/finance/account-admin.controller.ts" "$BASE_DIR/admin/finance/account.controller.ts"
cp "$BASE_DIR/finance/transaction-admin.controller.ts" "$BASE_DIR/admin/finance/transaction.controller.ts"
cp "$BASE_DIR/finance/withdrawal-admin.controller.ts" "$BASE_DIR/admin/finance/withdrawal.controller.ts"
cp "$BASE_DIR/finance/settlement-admin.controller.ts" "$BASE_DIR/admin/finance/settlement.controller.ts"
cp "$BASE_DIR/finance/reconciliation-admin.controller.ts" "$BASE_DIR/admin/finance/reconciliation.controller.ts"
cp "$BASE_DIR/finance/report-admin.controller.ts" "$BASE_DIR/admin/finance/report.controller.ts"

sed -i "s/@Controller('admin\/finance\/accounts')/@Controller('api\/admin\/finance\/accounts')/g" "$BASE_DIR/admin/finance/account.controller.ts"
sed -i "s/@Controller('admin\/finance\/transactions')/@Controller('api\/admin\/finance\/transactions')/g" "$BASE_DIR/admin/finance/transaction.controller.ts"
sed -i "s/@Controller('admin\/finance\/withdrawals')/@Controller('api\/admin\/finance\/withdrawals')/g" "$BASE_DIR/admin/finance/withdrawal.controller.ts"
sed -i "s/@Controller('admin\/finance\/settlements')/@Controller('api\/admin\/finance\/settlements')/g" "$BASE_DIR/admin/finance/settlement.controller.ts"
sed -i "s/@Controller('admin\/finance\/reconciliations')/@Controller('api\/admin\/finance\/reconciliations')/g" "$BASE_DIR/admin/finance/reconciliation.controller.ts"
sed -i "s/@Controller('admin\/finance\/reports')/@Controller('api\/admin\/finance\/reports')/g" "$BASE_DIR/admin/finance/report.controller.ts"

# ============================================
# 公共模块迁移
# ============================================
echo ""
echo "📦 迁移公共模块..."

# 1. shared/room
echo "  - 迁移 shared/room..."
cp "$BASE_DIR/room/room-public.controller.ts" "$BASE_DIR/shared/room/room.controller.ts"

sed -i "s/@Controller('rooms')/@Controller('api\/public\/rooms')/g" "$BASE_DIR/shared/room/room.controller.ts"
sed -i "s/RoomPublicController/RoomController/g" "$BASE_DIR/shared/room/room.controller.ts"

# 2. shared/merchant
echo "  - 迁移 shared/merchant..."
cp "$BASE_DIR/merchant/merchant-public.controller.ts" "$BASE_DIR/shared/merchant/merchant.controller.ts"

sed -i "s/@Controller('merchants')/@Controller('api\/public\/merchants')/g" "$BASE_DIR/shared/merchant/merchant.controller.ts"
sed -i "s/MerchantPublicController/MerchantController/g" "$BASE_DIR/shared/merchant/merchant.controller.ts"

# 3. shared/activity
echo "  - 迁移 shared/activity..."
cp "$BASE_DIR/activity/activity-public.controller.ts" "$BASE_DIR/shared/activity/activity.controller.ts"

sed -i "s/@Controller('activity')/@Controller('api\/public\/activity')/g" "$BASE_DIR/shared/activity/activity.controller.ts"
sed -i "s/ActivityPublicController/ActivityController/g" "$BASE_DIR/shared/activity/activity.controller.ts"

# 4. shared/upload
echo "  - 迁移 shared/upload..."
cp "$BASE_DIR/upload/upload.controller.ts" "$BASE_DIR/shared/upload/"
cp "$BASE_DIR/upload/upload.service.ts" "$BASE_DIR/shared/upload/"
cp "$BASE_DIR/upload/upload.module.ts" "$BASE_DIR/shared/upload/"

# 修改 upload controller 的多个路由前缀
sed -i "s/@Controller('upload')/@Controller('api\/user\/upload')/g" "$BASE_DIR/shared/upload/upload.controller.ts"
sed -i "s/@Controller('seller\/upload')/@Controller('api\/merchant\/upload')/g" "$BASE_DIR/shared/upload/upload.controller.ts"
sed -i "s/@Controller('admin\/upload')/@Controller('api\/admin\/upload')/g" "$BASE_DIR/shared/upload/upload.controller.ts"

echo ""
echo "✅ 文件复制和修改完成！"
echo ""
echo "⚠️  接下来需要："
echo "1. 创建各端的总模块文件（user.module.ts, merchant.module.ts等）"
echo "2. 更新 app.module.ts"
echo "3. 创建官网模块"
echo "4. 更新前端 API 路径"
