const { log } = require("handlebars");
const Blog = require("../models/Blog");
const User = require("../models/User");

module.exports.getAllBlogs = async (req, res, next) => {
  try {
    let blog = await Blog.find();
    if (blog) {
      res.json({ blogs: blog });
    } else {
      console.log("Error in Blog", blog);
      return;
    }
  } catch {
    (err) => {
      res.status(422).json({ error: err });
    };
  }
};

module.exports.getSingleBlog = async (req, res, next) => {
  try {
    const { id } = req.params;
    let blog = await Blog.findById({ _id: id });
    if (blog) {
      res.json({ blog });
    } else {
      res.json({ error: "Blog Not Found" });
    }
  } catch {
    (err) => {
      console.log("Error", err);
    };
  }
};

module.exports.addBlog = async (req, res, next) => {
  const { _id, title, body, author } = req.body; //_id is of user from frontend who is adding the blog.
  try {
    console.log("Image File", req.file);
    let user = await User.findById({ _id });
    if (!title) {
      res.status(422).json({ message: "Please, Add Title of the Blog" });
      return;
    }
    if (!body) {
      res.status(422).json({ message: "Please, Add Body of the Blog" });
      return;
    }
    if (user) {
      let blog = new Blog({
        title,
        body,
        image: req.file.location,
        date: new Date().toISOString(),
        author,
      });
      await blog.save();
      res.json({ message: "Blog Saved Successfully!" });
    } else {
      res.status.json({ error: "User Doesn't Exists" });
      return;
    }
  } catch {
    (err) => {
      res.status(422).json({ error: err });
    };
  }
};

module.exports.deleteBlog = async (req, res, next) => {
  const { _id } = req.body;
  try {
    let blog = await Blog.find({ _id });
    if (blog) {
      await Blog.findByIdAndDelete(_id);
      res.json({ message: "Blog Deleted!" });
    } else {
      res.status(404).json({ message: "Blog Not Found" });
      return;
    }
  } catch {
    (err) => {
      res.status(422).json({ error: err });
    };
  }
};

module.exports.editBlog = async (req, res, next) => {
  const { _id, body, title, image } = req.body;

  try {
    if (!title) {
      res.status(422).json({ message: "Please, Add the Title of the Blog." });
      return;
    }
    if (!body) {
      res.status(422).json({ message: "Please, Add the Body of the Blog." });
      return;
    }
    if (!image) {
      res.status(422).json({ message: "Please, Add the Image of the Blog." });
      return;
    }
    let blog = await Blog.findById({ _id });
    if (blog) {
      blog.title = title;
      blog.body = body;
      blog.image = image;
      blog = await blog.save();
      res.json({ message: "Blog Updated!" });
    } else {
      res.status(422).json({ error: "Blog Doesn't Found" });
      return;
    }
  } catch {
    (err) => {
      res.status(422).json({ error: err });
    };
  }
};
module.exports.commentBlog = async (req, res, next) => {
  const comment = req.body.comment;
  const today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1;

  var yyyy = today.getFullYear();
  if (dd < 10) {
    dd = "0" + dd;
  }
  if (mm < 10) {
    mm = "0" + mm;
  }
  var today = dd + "/" + mm + "/" + yyyy;

  const comments1 = {
    user: req.user._id,
    review: comment,
    date: datenow,
  };

  try {
    if (comment) {
      Blog.findByIdAndUpdate(
        { _id: req.params.id },
        {
          $push: {
            comments: comments1,
          },
        },
        function (err, docs) {
          if (err) {
            res.status(503).json({
              message: "internal server error cant post the comment",
              err,
            });
          } else {
            res.status(201).json({
              message: "Comment posted!",
            });
          }
        }
      );
    }
  } catch (err) {
    if (err) {
      res.status(501).json({
        message: "internal Server error please try again after some time",
        err,
      });
    }
  }
};