const mongoose = require('mongoose'); const bcrypt = require('bcryptjs'); const userSchema = new mongoose.Schema({ // Basic info username: { type: String, required: true, unique: true, trim: true, minlength: 3, maxlength: 30 }, email: { type: String, required: true, unique: true, trim: true, lowercase: true }, password: { type: String, required: true, minlength: 6 }, // Profile info profile: { firstName: { type: String, trim: true }, lastName: { type: String, trim: true }, ugaId: { type: String, trim: true }, // Student/Faculty ID year: { type: String, enum: ['Freshman', 'Sophomore', 'Junior', 'Senior', 'Graduate', 'Faculty', 'Staff'] }, major: { type: String, trim: true }, college: { type: String, trim: true }, avatar: { type: String }, // URL or base64 bio: { type: String, maxlength: 500 }, phoneNumber: { type: String, trim: true }, emergencyContact: { name: String, phone: String, relationship: String } }, // Academic info courses: [{ courseCode: String, // e.g., "CSCI 1301" courseName: String, instructor: String, semester: String, year: Number, grade: String }], // Settings settings: { theme: { type: String, enum: ['light', 'dark', 'auto'], default: 'light' }, notifications: { email: { type: Boolean, default: true }, push: { type: Boolean, default: true }, assignments: { type: Boolean, default: true }, events: { type: Boolean, default: true } }, privacy: { profileVisible: { type: Boolean, default: true }, showOnlineStatus: { type: Boolean, default: true } }, ai: { preferredModel: { type: String, default: 'llama3' }, webSearchEnabled: { type: Boolean, default: true }, contextLength: { type: Number, default: 4000 } } }, // Activity tracking lastLogin: { type: Date, default: Date.now }, lastActive: { type: Date, default: Date.now }, loginCount: { type: Number, default: 0 }, chatCount: { type: Number, default: 0 }, // Account status isActive: { type: Boolean, default: true }, isVerified: { type: Boolean, default: false }, role: { type: String, enum: ['student', 'faculty', 'staff', 'admin'], default: 'student' } }, { timestamps: true }); // Password hashing middleware userSchema.pre('save', async function(next) { if (!this.isModified('password')) return next(); try { const salt = await bcrypt.genSalt(12); this.password = await bcrypt.hash(this.password, salt); next(); } catch (error) { next(error); } }); // Password comparison method userSchema.methods.comparePassword = async function(candidatePassword) { return bcrypt.compare(candidatePassword, this.password); }; // Virtual for full name userSchema.virtual('fullName').get(function() { if (this.profile.firstName && this.profile.lastName) { return `${this.profile.firstName} ${this.profile.lastName}`; } return this.username; }); // Virtual for current courses userSchema.virtual('currentCourses').get(function() { const currentYear = new Date().getFullYear(); const currentSemester = new Date().getMonth() < 6 ? 'Spring' : 'Fall'; return this.courses.filter(course => course.year === currentYear && course.semester === currentSemester ); }); // Index for search userSchema.index({ username: 'text', email: 'text', 'profile.firstName': 'text', 'profile.lastName': 'text' }); module.exports = mongoose.model('User', userSchema);