import mongoose from 'mongoose'

const connectionSchema = new mongoose.Schema({
  initiator: { type: mongoose.Schema.Types.ObjectId, ref: 'Member', required: true },
  recipient: { type: mongoose.Schema.Types.ObjectId, ref: 'Member', required: true },
  status: { type: String, enum: ['pending', 'confirmed'], default: 'pending' },
  matchingTags: [
    {
      tagSlug: String,
      initiatorState: { type: String, enum: ['help', 'interested', 'seeking'] },
      recipientState: { type: String, enum: ['help', 'interested', 'seeking'] },
    },
  ],
  hiddenBy: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Member' }],
  createdAt: { type: Date, default: Date.now },
  confirmedAt: Date,
})

connectionSchema.index({ initiator: 1, recipient: 1 }, { unique: true })
connectionSchema.index({ recipient: 1, status: 1 })

export default mongoose.models.Connection || mongoose.model('Connection', connectionSchema)
