import { connectDB } from '@/lib/mongodb';
import { Users, IUser } from '@/models/Users';
import { Types } from 'mongoose';

export const userResolvers = {
    Query: {
        users: async () => {
            try {
                await connectDB();
                const users = await Users.find().populate("person").lean();
                return users;
            } catch (error) {
                console.log(error);
                return [];
            }
        },
    },
    Mutation: {
        createUser: async (parent: any, args: { input: IUser }) => {
            try {
                await connectDB();
                const user = new Users({ ...args.input, person: new Types.ObjectId(args.input.person) });
                await user.save();
                return user;
            } catch (error) {
                console.log(error);
                return null;
            }
        },
    },
};
