text: const express = require("express");
  const router = express.Router();
  const mongoose = require("mongoose");
  const passport = require("passport");
  
  const Profile = require("../../models/Profile");
  const User = require("../../models/User");
  const validateProfileInput = require("../../validation/profile");
  const validateExperienceInput = require("../../validation/experience");
  const validateEducationInput = require("../../validation/education");
  
  // @router    GET api/profile
  // @desc      Gets current user profile
  // @access    Private
  router.get(
    "/",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const errors = {};
      Profile.findOne({ user: req.user.id })
        .populate("user", ["name", "avatar"])
        .then(profile => {
          if (!profile) {
            errors.noprofile = "There is no profile for this user";
            return res.status(404).json(errors);
          }
          res.json(profile);
        })
        .catch(err => res.status(404).json(err));
    }
  );
  
  // @router    GET api/profile/all
  // @desc      Get all profiles
  // @access    Public
  router.get("/all", (req, res) => {
    const errors = {};
  
    Profile.find()
      .populate("name", ["name", "avatar"])
      .then(profiles => {
        if (!profiles) {
          errors.noprofile = "There are no profiles";
          return res.status(404).json(errors);
        }
        res.json(profiles);
      })
      .catch(err => {
        res.status(404).json({ noprofile: "There are no profiles" });
      });
  });
  
  // @router    GET api/profile/handle/:handle
  // @desc      Get profile by handle
  // @access    Public
  router.get("/handle/:handle", (req, res) => {
    const errors = {};
  
    Profile.findOne({ handle: req.params.handle })
      .populate("user", ["name", "avatar"])
      .then(profile => {
        if (!profile) {
          errors.profile = "There is no profile for this user";
          return res.status(404).json(errors);
        }
        res.json(profile);
      })
      .catch(err => res.status(404).json(err));
  });
  
  // @router    GET api/profile/user/:user_id
  // @desc      Get profile by user id
  // @access    Public
  router.get("/user/:user_id", (req, res) => {
    const errors = {};
  
    Profile.findOne({ user: req.params.user_id })
      .populate("user", ["name", "avatar"])
      .then(profile => {
        if (!profile) {
          errors.profile = "There is no profile for this user";
          return res.status(404).json(errors);
        }
        res.json(profile);
      })
      .catch(err =>
        res.status(404).json({ profile: "There is no profile for this user" })
      );
  });
  
  // @router    POST api/profile
  // @desc      Creates or edit user profile
  // @access    Private
  router.post(
    "/",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const { errors, isValid } = validateProfileInput(req.body);
  
      if (!isValid) return res.status(400).json(errors);
  
      // get fields
      const social = ["youtube", "instagram", "facebook", "linkedin", "twitter"];
      const entries = Object.entries(req.body);
      const profileFields = {};
      profileFields.social = {};
      profileFields.user = req.user.id;
      for (const [index, value] of entries) {
        if (index === "skills") profileFields[index] = value.split(",");
        else if (social.includes(index)) profileFields.social[index] = value;
        else profileFields[index] = value;
      }
  
      Profile.findOne({ user: req.user.id })
        .then(profile => {
          if (profile) {
            // update
            Profile.findOneAndUpdate(
              { user: req.user.id },
              { $set: profileFields },
              { new: true }
            )
              .then(profile => res.json(profile))
              .catch(err => res.json(err));
          } else {
            Profile.findOne({ handle: profileFields.handle })
              .then(profile => {
                if (profile) {
                  errors.handle = "That handle already exists";
                  res.status(400).json(errors);
                } else {
                  // save profile
                  new Profile(profileFields)
                    .save()
                    .then(profile => res.json(profile))
                    .catch(err => res.json(err));
                }
              })
              .catch(err => res.json(err));
          }
        })
        .catch(err => res.status(404).json(err));
    }
  );
  
  // @router    POST api/profile/experience
  // @desc      Add experience to profile
  // @access    Private
  router.post(
    "/experience",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const { errors, isValid } = validateExperienceInput(req.body);
  
      if (!isValid) return res.status(400).json(errors);
  
      Profile.findOne({ user: req.user.id })
        .then(profile => {
          const newExp = {
            title: req.body.title,
            company: req.body.company,
            location: req.body.location,
            from: req.body.from,
            to: req.body.to,
            current: req.body.current,
            description: req.body.description
          };
  
          // add to experience array
          profile.experience.unshift(newExp);
  
          profile
            .save()
            .then(profile => res.json(profile))
            .catch();
        })
        .catch();
    }
  );
  
  // @router    POST api/profile/education
  // @desc      Add education to profile
  // @access    Private
  router.post(
    "/education",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const { errors, isValid } = validateEducationInput(req.body);
  
      if (!isValid) return res.status(400).json(errors);
  
      Profile.findOne({ user: req.user.id })
        .then(profile => {
          const newEdu = {
            school: req.body.school,
            degree: req.body.degree,
            fieldofstudy: req.body.fieldofstudy,
            from: req.body.from,
            to: req.body.to,
            current: req.body.current,
            description: req.body.description
          };
  
          // add to experience array
          profile.education.unshift(newEdu);
  
          profile
            .save()
            .then(profile => res.json(profile))
            .catch();
        })
        .catch();
    }
  );
  
  // @router    DELETE api/profile/experience/:exp_id
  // @desc      Delete experience from profile by id
  // @access    Private
  router.delete(
    "/experience/:exp_id",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      Profile.findOne({ user: req.user.id })
        .then(profile => {
          // get remove index
          const removeIndex = profile.experience
            .map(item => item.id)
            .indexOf(req.params.exp_id);
  
          // splice from array and save
          profile.experience.splice(removeIndex, 1);
          profile
            .save()
            .then(profile => res.json(profile))
            .catch(err => res.status(404).json(err));
        })
        .catch();
    }
  );
  
  // @router    DELETE api/profile/education/:edu_id
  // @desc      Delete experience from profile by id
  // @access    Private
  router.delete(
    "/education/:edu_id",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      Profile.findOne({ user: req.user.id })
        .then(profile => {
          // get remove index
          const removeIndex = profile.education
            .map(item => item.id)
            .indexOf(req.params.edu_id);
  
          // splice from array and save
          profile.education.splice(removeIndex, 1);
          profile
            .save()
            .then(profile => res.json(profile))
            .catch(err => res.status(404).json(err));
        })
        .catch();
    }
  );
  
  // @router    DELETE api/profile/
  // @desc      Delete user and profile
  // @access    Private
  router.delete(
    "/",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      Profile.findOneAndRemove({ user: req.user.id })
        .then(() => {
          User.findOneAndRemove({ _id: req.user.id })
            .then(() => res.json({ success: true }))
            .catch(err => res.json(err));
        })
        .catch(err => res.json(err));
    }
  );
  
  module.exports = router;
  