text: require('dotenv').config()
  const router = require("express").Router();
  const Post = require("../models/Post");
  const User = require('../models/User');
  
  
  //create a post
  
  router.post("/", async(req,res)=>{
  
      const newPost = new Post(req.body)
      try{
          const savedPost = await newPost.save();
          res.status(201).json(savedPost);
      } catch(err){
          res.status(500).json(err);
      }
  
  
  });
  //update a post
  
  router.put("/:id", async (req, res) => {
      try {
        const post = await Post.findById(req.params.id);
        if (post.userId === req.body.userId) {
          await post.updateOne({ $set: req.body });
          res.status(200).json(" Post has been updated successfully!");
        } else {
          res.status(403).json("Error! You can update only your post");
        }
      } catch (err) {
        res.status(500).json(err);
      }
    });
  
  //delete a post
  router.delete("/:id", async (req, res) => {
      try {
        const post = await Post.findById(req.params.id);
        if (post.userId === req.body.userId) {
          await post.deleteOne();
          res.status(200).json(" Post has been deleted successfully!");
        } else {
          res.status(403).json("Error! You can delete only your post");
        }
      } catch (err) {
        res.status(500).json(err);
      }
    });
  
  //like a post
  
  router.put("/:id/like", async (req, res) => {
      try {
        const post = await Post.findById(req.params.id);
        if (!post.likes.includes(req.body.userId)) {
          await post.updateOne({ $push: { likes: req.body.userId } });
          res.status(200).json(" Post liked");
        } else {
          await post.updateOne({ $pull: { likes: req.body.userId } });
          res.status(200).json("Post disliked");
        }
      } catch (err) {
        res.status(500).json(err);
      }
    });
  // get a post
  router.get("/:id", async (req,res)=>{
  
      try{
          const post = await Post.findById(req.params.id);
          res.status(200).json(post);
      } catch(err){
          res.status(500).json(err);
      }
  });
  //get timeline users
  router.get("/timeline/all", async (req,res)=>{
      try{
          const currentUser = await User.findById(req.body.userId);
          const userPosts = await Post.find({userId: currentUser._id});
          const friendPosts = await Promise.all(
              currentUser.followings.map((friendId) => {
                  return Post.find({userId: friendId});
              })
          );
          res.status(200).json(userPosts.concat(...friendPosts));
      } catch(err){
          res.status(500).json(err);
      }
  
  
  });
  
  //exporting to server file
  module.exports = router;