import mongoose, { type InferSchemaType } from 'mongoose'

const participantSchema = new mongoose.Schema(
  {
    meetingId: { type: mongoose.Schema.Types.ObjectId, ref: 'Meeting', required: true, index: true },
    userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null },
    guestId: { type: String, default: null },
    guestName: { type: String, default: null, maxlength: 80 },
    displayName: { type: String, required: true, maxlength: 80 },
    role: { type: String, enum: ['host', 'participant'], required: true },
    status: { type: String, enum: ['waiting', 'admitted', 'left', 'removed'], default: 'admitted', index: true },
    joinedAt: { type: Date, default: null },
    leftAt: { type: Date, default: null },
  },
  { timestamps: { createdAt: true, updatedAt: false } },
)

participantSchema.index({ meetingId: 1, userId: 1 })
participantSchema.index({ meetingId: 1, guestId: 1 })

export type ParticipantDocument = InferSchemaType<typeof participantSchema> & { _id: mongoose.Types.ObjectId }

export const Participant = mongoose.model('Participant', participantSchema)
