text: const mongoose = require('mongoose');
  const _ = require('lodash');
  const generateSlug = require('../utils/slugify');
  const sendEmail = require('../aws-ses');
  const { getEmailTemplate } = require('./EmailTemplate');
  
  const { Schema } = mongoose;
  
  const mongoSchema = new Schema({
    googleId: {
      type: String,
      required: true,
      unique: true,
    },
    googleToken: {
      access_token: String,
      refresh_token: String,
      token_type: String,
      expiry_date: Number,
    },
    slug: {
      type: String,
      required: true,
      unique: true,
    },
    createdAt: {
      type: Date,
      required: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
    },
    isAdmin: {
      type: Boolean,
      default: false,
    },
    displayName: String,
    avatarUrl: String,
  });
  
  class UserClass {
    static publicFields() {
      return ['id', 'displayName', 'email', 'avatarUrl', 'slug', 'isAdmin'];
    }
  
    static async signInOrSignUp({ googleId, email, googleToken, displayName, avatarUrl }) {
      const user = await this.findOne({ googleId }).select(UserClass.publicFields().join(' '));
  
      if (user) {
        const modifier = {};
  
        if (googleToken.accessToken) {
          modifier.access_token = googleToken.accessToken;
        }
  
        if (googleToken.refreshToken) {
          modifier.refresh_token = googleToken.refreshToken;
        }
  
        if (_.isEmpty(modifier)) {
          return user;
        }
  
        await this.updateOne({ googleId }, { $set: modifier });
  
        return user;
      }
  
      const slug = await generateSlug(this, displayName);
      const userCount = await this.find().countDocuments();
  
      const newUser = await this.create({
        createdAt: new Date(),
        googleId,
        email,
        googleToken,
        displayName,
        avatarUrl,
        slug,
        isAdmin: userCount === 0,
      });
  
      try {
        const template = await getEmailTemplate('welcome', {
          userName: displayName,
        });
  
        await sendEmail({
          from: `Kelly from Builder Book <${process.env.EMAIL_ADDRESS_FROM}>`,
          to: [email],
          subject: template.subject,
          body: template.message,
        });
      } catch (err) {
        console.error('Email sending error:', err);
      }
  
      return _.pick(newUser, UserClass.publicFields());
    }
  }
  
  mongoSchema.loadClass(UserClass);
  
  const User = mongoose.model('User', mongoSchema);
  
  module.exports = User;
  