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

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

export interface IUser extends Document {
    clerkId?: string;
    logtoId?: string;
    email: string;
    name: string;
    role: UserRole;
    isBanned: boolean;
    organizationId?: string;
    createdAt: Date;
    updatedAt: Date;
    lastSeenAt?: Date;
}

const UserSchema: Schema = new Schema({
    clerkId: {
        type: String,
        required: false,
        unique: true,
        sparse: true,
        index: true
    },
    logtoId: {
        type: String,
        required: false,
        unique: true,
        sparse: true,
        index: true
    },
    organizationId: {
        type: String,
        index: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    name: {
        type: String,
        required: true
    },
    role: {
        type: String,
        enum: ['guest', 'user', 'admin'],
        default: 'guest'
    },
    isBanned: {
        type: Boolean,
        default: false
    },
    lastSeenAt: {
        type: Date,
        default: Date.now
    }
}, {
    timestamps: true
});

export default mongoose.models.User || mongoose.model<IUser>('User', UserSchema);
