import { Router } from 'express';
import { OrderController } from '../controllers/OrderController';
import { requireTraceContext } from '../../core/guards/trace-context.guard';
import { requirePermission } from '../../core/guards/rbac.guard';

const router = Router();

/**
 * [BIZ_OPS_01] 多平台订单实时同步 API
 */

// 平台 Webhook 接收 (不强制 traceContext，因为由平台推送，但可根据 Header 识别租户)
router.post('/webhook/:platform', OrderController.handlePlatformWebhook);

// 手动触发同步 (需权限)
router.post('/sync', requireTraceContext, requirePermission('trade:write'), OrderController.triggerManualSync);

// 获取订单统计 (需权限)
router.get('/stats', requireTraceContext, requirePermission('trade:read'), OrderController.getStats);

/**
 * [ERP_ORD_01] 订单管理 API
 */

// 创建订单
router.post('/', requireTraceContext, requirePermission('order:write'), OrderController.createOrder);

// 获取订单详情
router.get('/:id', requireTraceContext, requirePermission('order:read'), OrderController.getOrderById);

// 更新订单
router.put('/:id', requireTraceContext, requirePermission('order:write'), OrderController.updateOrder);

// 删除订单
router.delete('/:id', requireTraceContext, requirePermission('order:delete'), OrderController.deleteOrder);

// 获取订单列表
router.get('/', requireTraceContext, requirePermission('order:read'), OrderController.getOrders);

// 批量更新订单
router.put('/batch', requireTraceContext, requirePermission('order:write'), OrderController.batchUpdateOrders);

// 订单状态流转
router.post('/:id/status', requireTraceContext, requirePermission('order:write'), OrderController.transitionOrderStatus);

// 批量审核订单
router.post('/batch/audit', requireTraceContext, requirePermission('order:write'), OrderController.batchAuditOrders);

// 批量发货
router.post('/batch/ship', requireTraceContext, requirePermission('order:write'), OrderController.batchShipOrders);

// 标记订单为异常
router.post('/:id/exception', requireTraceContext, requirePermission('order:write'), OrderController.markOrderAsException);

// 自动改派订单
router.post('/:id/reroute', requireTraceContext, requirePermission('order:write'), OrderController.autoRerouteOrder);

// 重试异常订单
router.post('/:id/retry', requireTraceContext, requirePermission('order:write'), OrderController.retryExceptionOrder);

// 取消订单
router.post('/:id/cancel', requireTraceContext, requirePermission('order:write'), OrderController.cancelOrder);

// 申请退款
router.post('/:id/refund', requireTraceContext, requirePermission('order:write'), OrderController.requestRefund);

// 审批退款
router.post('/refund/:id/approve', requireTraceContext, requirePermission('order:write'), OrderController.approveRefund);

// 申请售后
router.post('/:id/after-sales', requireTraceContext, requirePermission('order:write'), OrderController.requestAfterSales);

// 处理售后申请
router.post('/after-sales/:id/process', requireTraceContext, requirePermission('order:write'), OrderController.processAfterSales);

// 完成订单
router.post('/:id/complete', requireTraceContext, requirePermission('order:write'), OrderController.completeOrder);

export default router;
