import mongoose, { Schema, Document } from 'mongoose';

export interface IIntegrationIdempotencyDocument extends Document {
  key: string;
  method: string;
  path: string;
  statusCode: number;
  responsePayload: any;
  resourceType?: string;
  resourceId?: string;
  createdAt: Date;
  updatedAt: Date;
}

const integrationIdempotencySchema = new Schema<IIntegrationIdempotencyDocument>(
  {
    key: { type: String, required: true, unique: true },
    method: { type: String, required: true },
    path: { type: String, required: true },
    statusCode: { type: Number, required: true },
    responsePayload: { type: Schema.Types.Mixed, required: true },
    resourceType: String,
    resourceId: String,
  },
  { timestamps: true }
);

integrationIdempotencySchema.index({ createdAt: 1 }, { expireAfterSeconds: 60 * 60 * 24 * 30 });

export const IntegrationIdempotency = mongoose.model<IIntegrationIdempotencyDocument>('IntegrationIdempotency', integrationIdempotencySchema);