import { Schema, model, Document } from "mongoose";

// Interface for MessageRecord document
export interface IMessageRecord extends Document {
  uid: string; // Storing as string to match User model's uid
  activityId?: Schema.Types.ObjectId;
  activityName?: string;
  templateId?: Schema.Types.ObjectId;
  templateName?: string;
  strategyId?: Schema.Types.ObjectId;
  strategyName?: string;
  title: string;
  content: string;
  image?: string;
  bigger: boolean;
  action?: string;
  param?: string;
  extend?: string;
  status: number;
  inforeground?: boolean;
  errno?: string;
  fcmReceipt?: string;
  plannedSendAt?: Date; // 计划发送时间
  actualSendAt?: Date; // 实际调用firebase admin api发送的时间
  deliveredAt?: Date; // 客户端接收到消息的时间
  openedAt?: Date; // 消息被打开的时间
  createdAt: Date;
  updatedAt: Date;
}

// Mongoose schema for MessageRecord
const messageRecordSchema = new Schema<IMessageRecord>(
  {
    // 目标用户uid
    uid: {
      type: String,
      required: true,
      index: true, // Index for faster lookup by user
    },
    // 关联消息活动, 关联到消息活动表（messageActivity），表明次消息记录来自于那个宣传活动；有可能是空值，对于点对点发送消息的情况可能没有关联的活动
    activityId: {
      type: Schema.Types.ObjectId,
      ref: "MessageActivity",
      required: false, // It's optional as per design
      index: true, // Added index for better query performance by activity
    },

    // 关联消息活动名称
    activityName: {
      type: String,
      index: true,
    },

    // 关联的消息模版id，也可能是空值，对于点对点发送消息的情况可能没有模版
    templateId: {
      type: Schema.Types.ObjectId,
      ref: "MessageTemplate",
      required: false, // It's optional as per design
    },

    // 关联的消息模板名称
    templateName: {
      type: String,
      index: true,
    },

    // 关联的策略id，也可能是空值，对于点对点发送消息的情况可能没有模版
    strategyId: {
      type: Schema.Types.ObjectId,
      ref: "MessageTemplate",
      required: false, // It's optional as per design
    },

    // 关联的消息策略名称
    strategyName: {
      type: String,
      index: true,
    },

    // 已经确定了具体语言的消息标题，必须
    title: {
      type: String,
      required: true,
      index: true,
    },
    // 已经确定了具体语言的消息内容，必须
    content: {
      type: String,
      required: true,
    },
    // 图片url， 非必须
    image: {
      type: String,
    },
    // 消息通知的消息是否点击展开查看大图。没有图片的情况下一般就设置成false
    bigger: {
      type: Boolean,
      default: false,
    },
    // 定义客户端收到消息后的行为，如 go/app ， open/art 参看前文
    action: {
      type: String,
    },
    // 消息参数
    param: {
      type: String,
    },
    // 消息扩展参数
    extend: {
      type: String,
    },
    // 0-未发送；1-发送成功；2-已送达；3-已打开；-1-发送失败
    status: {
      type: Number,
      default: 0, // 0-not sent; 1-sent successfully; 2-delivered; 3-opened; -1-send failed
      index: true,
    },
    // true: 在前台； false: 在后台
    inforeground: {
      type: Boolean,
    },
    // 捕捉firebase admin sdk api调用失败的信息填充到这里
    errno: {
      type: String,
    },
    // 消息发送成功的回执，其实就是FCM用于标识消息的id
    fcmReceipt: {
      type: String,
      index: true, // Index for faster lookup by FCM receipt
    },
    // 新增字段
    plannedSendAt: {
      type: Date,
      index: true,
    },
    actualSendAt: {
      type: Date,
      index: true,
    },
    deliveredAt: {
      type: Date,
      index: true,
    },
    openedAt: {
      type: Date,
      index: true,
    },
  },
  {
    timestamps: true, // Automatically adds createdAt and updatedAt
  }
);

// Added compound index for common queries by user and status
messageRecordSchema.index({ uid: 1, status: 1 });

export const MessageRecord = model<IMessageRecord>("MessageRecord", messageRecordSchema);
