import { z } from "zod";

export enum TradeWayValueEnum {
  LIMIT = "limit",
  MARKET = "market",
}

export type TradeWayValue = `${TradeWayValueEnum}`;

export interface TradeWayConfig {
  name: string;
  value: TradeWayValue;
  description: string;
  icon: string;
}

export const tradeWayConfig: TradeWayConfig[] = [
  {
    name: "限价委托",
    value: "limit",
    description: "以指定价格买入或卖出",
    icon: "hugeicons:trade-up",
  },
  {
    name: "市价委托",
    value: "market",
    description: "以市场价格买入或卖出",
    icon: "hugeicons:trade-down",
  },
];

export const confirmOrderSchema = z.object({
  quantity: z.coerce.number({ message: "请输入有效的数量" }).gt(0, "数量必须大于0"),
  price: z.coerce.number({ message: "请输入有效的价格" }).gt(0, "价格必须大于0").optional().or(z.coerce.number().optional()),
  orderType: z.enum([TradeWayValueEnum.LIMIT, TradeWayValueEnum.MARKET], {
    message: "请选择有效的交易方式",
  }) as z.ZodType<TradeWayValue>,
}).refine(
  (data) => {
    if (data.orderType === TradeWayValueEnum.LIMIT) {
      return data.price !== undefined && data.price > 0;
    }
    return true;
  },
  {
    message: "价格为必填项",
    path: ["price"],
  },
);

export const confirmOrderSubmitSchema = confirmOrderSchema.transform(data => ({
  ...data,
  quantity: data.quantity.toString(),
  price: data.price?.toString() ?? "",
}));

export enum OrderStatusEnum {
  PENDING = "pending",
  OPEN = "open",
  PARTIALLY_FILLED = "partially_filled",
  FILLED = "filled",
  CANCELED = "cancelled",
  REJECTED = "rejected",
}

export const orderStatusMap = {
  [OrderStatusEnum.PENDING]: {
    color: "warning",
    text: "待处理",
  },
  [OrderStatusEnum.OPEN]: {
    color: "primary",
    text: "已挂单",
  },
  [OrderStatusEnum.PARTIALLY_FILLED]: {
    color: "tertiary",
    text: "部分成交",
  },
  [OrderStatusEnum.FILLED]: {
    color: "success",
    text: "已完成",
  },
  [OrderStatusEnum.CANCELED]: {
    color: "medium",
    text: "已取消",
  },
  [OrderStatusEnum.REJECTED]: {
    color: "danger",
    text: "已拒绝",
  },
};
