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

export type OrgRole = 'guest' | 'user' | 'admin';

export interface IOrganizationMember extends Document {
    clerkUserId: string;
    organizationId: string;
    role: OrgRole;
    isBanned: boolean;
    // Denormalized user info for quick access
    email: string;
    name: string;
    createdAt: Date;
    updatedAt: Date;
}

const OrganizationMemberSchema: Schema = new Schema({
    clerkUserId: {
        type: String,
        required: true,
        index: true
    },
    organizationId: {
        type: String,
        required: true,
        index: true
    },
    role: {
        type: String,
        enum: ['guest', 'user', 'admin'],
        default: 'guest'
    },
    isBanned: {
        type: Boolean,
        default: false
    },
    email: {
        type: String,
        required: true
    },
    name: {
        type: String,
        required: true
    }
}, {
    timestamps: true
});

// Compound index for unique user per organization
OrganizationMemberSchema.index({ clerkUserId: 1, organizationId: 1 }, { unique: true });

export default mongoose.models.OrganizationMember || mongoose.model<IOrganizationMember>('OrganizationMember', OrganizationMemberSchema);
