import jwt from "jsonwebtoken";
import user from "../models/user.js";
import bcryptjs from 'bcryptjs'



export const signup=async(req,res)=>{
    try {
        const{UserId,username,email,password}=req.body;
        const userExists=await user.findOne({email});

        if(userExists){
            return res.status(400).json({message:"user already exists"});
        }

        const hashed= await bcryptjs.hash(password,10);

        await new user({UserId,username,email,password:hashed}).save();

        res.status(201).json({message:"user created successfully"});
    } catch (error) {
        res.status(500).json({message:"internal server error",error:error.message})
    }
};



export const login=async(req,res)=>{
    try {
         const{email,password}=req.body;
        const userExists=await user.findOne({email});

        if(!userExists){
             return res.status(404).json({message:"user not found"});
        }

        const verifypassword=await bcryptjs.compare(password,userExists.password)

        if(!verifypassword){
             return res.status(404).json({message:"wrong password"});
        }
       const token=jwt.sign({UserId:userExists.UserId,email:userExists.email},"secretkey",{expiresIn:"1d"})
        res.status(200).json({message:"user logged in successfully",token,username:userExists.username})


    } catch (error) {
        res.status(500).json({message:"internal server error",error:error.message})
    }
};

export const getAllProfiles=async(req,res)=>{
    try {
        const userExists= await user.find()
        if(!userExists){
            return res.status(404).json({message:"no users found"})
        }

        res.status(200).json(userExists)
    } catch (error) {
         res.status(500).json({message:"internal server error",error:error.message})
    }
}


export const getProfile=async(req,res)=>{
    try {
        const userExists= await user.findById(req.params.id);
        if(!userExists){
            return res.status(404).json({mesage:"user not found"});
        }

         res.status(200).json({user:{
            id:userExists.id,
            names:userExists.username,
            email:userExists.email
         }})
    } catch (error) {
         res.status(500).json({message:"internal server error",error:error.message})
    }
};


export const updateUser=async(req,res)=>{
    try{
        const{names,email}=req.body;
        const userExists=await user.findByIdAndUpdate(req.params.id,req.body,{new:true}
        );
        if (!userExists) {
            return res.status(404).json({message:"user not found"})
            
        }
        res.status(200).json(userExists)

    } catch(error){
         res.status(500).json({message:"internal server error",error:error.message})

    }
};


export const deleteUser=async(req,res)=>{
    try {
        const userExists=await user.findByIdAndDelete(req.params.id)
        if(!userExists){
            return res.status(404).json({message:"user not found"})

        }
        res.status(200).json({message:"user deleted successfully"})
    } catch (error) {
        res.status(500).json({message:"internal server error",error:error.message})
    }
}