import { Schema, model, Document } from 'mongoose';
import { IWebhookRequest } from './webhook.interface';

const webhookRequestSchema = new Schema<IWebhookRequest>(
  {
    companyId: {
      type: String,
      required: true,
      trim: true,
    },
    method: {
      type: String,
      required: true,
      enum: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
    },
    url: {
      type: String,
      required: true,
    },
    headers: {
      type: Object,
      required: true,
    },
    body: {
      type: Schema.Types.Mixed,
      required: true,
    },
    query: {
      type: Object,
      default: {},
    },
    params: {
      type: Object,
      default: {},
    },
    ip: {
      type: String,
      default: null,
    },
    userAgent: {
      type: String,
      default: null,
    },
    status: {
      type: String,
      enum: ['received', 'processed', 'failed'],
      default: 'received',
    },
    processingError: {
      type: String,
      default: null,
    },
  },
  {
    timestamps: true,
  }
);

// Index for performance
webhookRequestSchema.index({ companyId: 1, createdAt: -1 });
webhookRequestSchema.index({ status: 1, createdAt: -1 });

export const WebhookRequest = model<IWebhookRequest>('WebhookRequest', webhookRequestSchema);
