import express from 'express';
import { protect } from "../middlewares/auth.js";
import User from "../models/userModel.js";
import jwt from 'jsonwebtoken';

const router =  express.Router();

router.post('/register',async (req, res) =>{
    const {username,email,password} =req.body;
    try{
    if(!username || !email || !password){
         return res.status(400).json({message:"Please fill all the fields"});
    }

    const userExists = await User.findOne({email});
    if(userExists){
        return res.status(400).json({message:"User already exists"});
    }

    const user = await User.create({username, email, password});

    const token = generateToken(user._id);

    res.status(201).json({
        id: user._id,
        username: user.username,
        email: user.email,
        token
    });
}catch(err){
    console.error(err);
    res.status(500).json({message:"Server error"});
}
});

router.post('/login', async(req, res) =>{
    const {email,password} = req.body;

  try{
    if(!email || !password){
        return res.status(400).json({message:"please Fill out all fields"});
    }
    const user = await User.findOne({email});

    if(!user || !(await user.matchPassword(password))){
        return res.status(401).json({message:"Invalid passwword"});
    }

    const token = generateToken(user._id);

    res.status(200).json({
        id:user._id,
        username: user.username,
        email:user.email,
        token 
    });

  } catch(err){
    console.error(err);
    res.status(500).json ({ message: "Server  error"});
  }
});


router.get('/me',protect, async(req, res)=>{
    res.status(200).json(req.user);
});

const generateToken = (id) => {
    return jwt.sign({id}, process.env.JWT_TOKEN, {expiresIn: '30d'});
}

export default router;

