/**
 * 文件职责：订单大厅卡片操作动作（接单、拒单、出餐、确认送达）。
 */
import { message } from 'ant-design-vue';

import {
  acceptOrderApi,
  completePreparationApi,
  confirmDeliveryApi,
  rejectOrderApi,
} from '#/api/order-hall';

/** 卡片操作动作选项。 */
interface CardActionsOptions {
  onRefresh: () => Promise<void>;
  onAccepted?: () => void;
}

/** 创建卡片操作动作。 */
export function createCardActions(options: CardActionsOptions) {
  const { onRefresh, onAccepted } = options;

  // 1. 接单
  async function handleAccept(orderId: string): Promise<void> {
    await acceptOrderApi(orderId);
    message.success('接单成功');
    onAccepted?.();
    await onRefresh();
  }

  // 2. 拒单
  async function handleReject(orderId: string, reason: string): Promise<void> {
    await rejectOrderApi(orderId, { reason });
    message.success('已拒单');
    await onRefresh();
  }

  // 3. 出餐完成
  async function handleCompletePreparation(orderId: string): Promise<void> {
    await completePreparationApi(orderId);
    message.success('出餐完成');
    await onRefresh();
  }

  // 4. 确认送达/取餐
  async function handleConfirmDelivery(orderId: string): Promise<void> {
    await confirmDeliveryApi(orderId);
    message.success('已确认送达');
    await onRefresh();
  }

  return {
    handleAccept,
    handleReject,
    handleCompletePreparation,
    handleConfirmDelivery,
  };
}
