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

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

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

const UserSchema: Schema = new Schema({
    externalId: {
        type: String,
        required: true,
        unique: true,
        index: true
    },
    passwordHash: {
        type: String,
        required: false
    },
    organizationId: {
        type: String,
        index: true
    },
    email: {
        type: String,
        required: 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);
