import { Prisma } from "@prisma/client";
import { prisma } from "@/lib/prisma";
import type { AlertDetail, AlertType } from "@/modules/alert/types";

export type WriteAlertOptions = {
  quoteId?: string;
  cargoHash?: string;
  detail?: AlertDetail;
};

/**
 * 通用预警写入（task-050）
 * 失败仅 log，不阻断主流程
 */
export async function writeAlert(
  alertType: AlertType,
  options?: WriteAlertOptions,
): Promise<void> {
  try {
    await prisma.alertLog.create({
      data: {
        alertType,
        quoteId: options?.quoteId ?? null,
        cargoHash: options?.cargoHash ?? null,
        detailJson: options?.detail
          ? (options.detail as Prisma.InputJsonValue)
          : undefined,
        status: "open",
      },
    });
  } catch (error) {
    console.error(`[alert] 写入 ${alertType} 失败:`, error);
  }
}
