text: import asyncHandler from 'express-async-handler'
  import User from '../models/userModel.js'
  import generateToken from '../utils/generateToken.js'
  // @desc auth user & get token
  // @route POST /api/users/login
  // @access Public
  const authUser = asyncHandler(async(req,res) => {
      const {email, password} = req.body
      const user = await User.findOne({email: email})
  
      if(user && (await user.matchPassword(password))) {
          res.json({
              _id: user._id,
              name: user.name,
              email:user.email,
              isAdmin: user.isAdmin,
              token: generateToken(user._id)
          })
      }
      else {
          res.status(401)
          throw new Error('Invalid email of password')
      }
  })
  
  // @desc Register a new user
  // @route POST /api/users
  // @access Public
  const registerUser = asyncHandler(async(req,res) => {
      const {name,email, password} = req.body
      const userExists = await User.findOne({email: email})
  
      if(userExists) {
          res.status(400)
          throw new Error('User already exists')
      }
      
      const user = await User.create({
          name,
          email,
          password
      })
  
      if(user) {
          res.status(201).json({
              _id: user._id,
              name: user.name,
              email:user.email,
              isAdmin: user.isAdmin,
              token: generateToken(user._id)
          })
      }
      else {
          res.status(400)
          throw new Error('Invalid user data')
      }
  })
  
  // @desc    Get user profile
  // @route   GET /api/users/profile
  // @access  Private
  const getUserProfile = asyncHandler(async (req, res) => {
      const user = await User.findById(req.user._id)
    
      if (user) {
        res.json({
          _id: user._id,
          name: user.name,
          email: user.email,
          isAdmin: user.isAdmin,
        })
      } else {
        res.status(404)
        throw new Error('User not found')
      }
    })
  
  // @desc    update user profile
  // @route   PUT /api/users/profile
  // @access  Private
  const updateUserProfile = asyncHandler(async (req, res) => {
      const user = await User.findById(req.user._id)
    
      if (user) {
        user.name = req.body.name || user.name
        user.email = req.body.email || user.email
        if(req.body.password) {
            user.password = req.body.password
        }
  
        const updatedUser = await user.save()
  
      res.json({
          _id: updatedUser._id,
          name: updatedUser.name,
          email:updatedUser.email,
          isAdmin: updatedUser.isAdmin,
          token: generateToken(updatedUser._id)
      })
      } else {
        res.status(404)
        throw new Error('User not found')
      }
    })
  
  // @desc    Get all users
  // @route   GET /api/users
  // @access  Private/Admin
  const getUsers = asyncHandler(async (req, res) => {
    const users = await User.find({})
    res.json(users)
  })
  
  
  // @desc    Delete a user
  // @route   GET /api/users/:id
  // @access  Private/Admin
  const deleteUsers = asyncHandler(async (req, res) => {
    const user = await User.findById(req.params.id)
    if(user){
      await user.remove()
      res.json({message:"User Removed"})
    }
    else{
      res.status(404)
      throw new Error('User Not found')
    }
  })
  
  // @desc    Get  user by id
  // @route   GET /api/users/:id
  // @access  Private/Admin
  const getUserById = asyncHandler(async (req, res) => {
    const user = await User.findById(req.params.id).select('-password')
    if (user) {
      res.json(user)
    }
    else {
      res.status(404)
      throw new Error('User Not found')
    }
  })
  
  // @desc    update user 
  // @route   PUT /api/users/:id
  // @access  Private/Admin
  const updateUser = asyncHandler(async (req, res) => {
    const user = await User.findById(req.params.id)
  
    if (user) {
      user.name = req.body.name || user.name
      user.email = req.body.email || user.email
      user.isAdmin = req.body.isAdmin 
  
      const updatedUser = await user.save()
  
    res.json({
        _id: updatedUser._id,
        name: updatedUser.name,
        email:updatedUser.email,
        isAdmin: updatedUser.isAdmin,
    })
    } else {
      res.status(404)
      throw new Error('User not found')
    }
  })
  
  
  export {authUser,getUserProfile,registerUser,updateUserProfile,getUsers,deleteUsers,getUserById,updateUser}