text: const express = require(`express`);
  const router = express.Router();
  const mongoose = require(`mongoose`);
  const passport = require("passport");
  
  //load validation
  const validateProfileInput = require("../../validation/profile");
  const validateExperienceInput = require("../../validation/experience");
  const validateEducationInput = require("../../validation/education");
  
  //load Profile model
  const Profile = require(`../../models/Profile`);
  
  //load user profile
  const user = require(`../../models/User`);
  //@route  get api/profile/test
  //@desc   Test profile route
  //@access   public
  
  router.get("/test", (req, res) => res.json({ msg: "profile works" }));
  
  //@route  get api/profile
  //@desc   get current user's profile
  //@access   private
  
  router.get(
    `/`,
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const errors = {};
  
      Profile.findOne({ user: req.user.id })
        .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));
    }
  );
  
  //@route  post 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.noprofile = "there is no profile for this user";
          res.status(404).json(errors);
        }
        res.json(profile);
      })
      .catch(err => res.status(400).json(err));
  });
  
  //@route  post api/profile/handle/: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.noprofile = "there is no profile for this user";
          res.status(404).json(errors);
        }
        res.json(profile);
      })
      .catch(err => res.status(400).json(err));
  });
  //@route  post api/profile/all
  //@desc   get all profiles
  //@access   public
  
  router.get("/all", (req, res) => {
    const errors = {};
    Profile.find()
      .populate("user", ["name"], ["avatar"])
      .then(profiles => {
        if (!profiles) {
          errors.noprofile = "there are no registered profiles";
          return res.status(404).json(errors);
        }
        res.json(profiles);
      })
      .catch(err => res.status(404).json({ profile: "there are no profiles" }));
  });
  
  //@route  post api/profile
  //@desc   create or edit user profile
  //@access   private
  
  router.post(
    `/`,
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const { errors, isValid } = validateProfileInput(req.body);
  
      //check validation
      if (!isValid) {
        //return any errors with 400 status
        return res.status(400).json(errors);
      }
      //get fields needed to create a profile
  
      //everything coming in will be put in an object called profilefields to be filled by all the stuff from the form
      const profileFields = {};
  
      profileFields.user = req.user.id; //to identify the logged in user. includes avatar, name and email
  
      if (req.body.handle) profileFields.handle = req.body.handle;
      if (req.body.company) profileFields.company = req.body.company;
      if (req.body.website) profileFields.website = req.body.website;
      if (req.body.location) profileFields.hlocation = req.body.location;
      if (req.body.status) profileFields.status = req.body.status;
      if (req.body.githubusername)
        profileFields.githubusername = req.body.githubusername;
  
      /*skills is going to come in as an array since one user can have multiple skills. these skills will come in sepearated by commas, so to get them, i need to identify all those values after the comma.
  
  so we need to first test if the skills array is not empty, and if it is not, then we need to collect the values after every comma as a single string object*/
  
      if (typeof req.body.skills !== "undefined") {
        profileFields.skills = req.body.skills.split(",");
      }
  
      //social.
      //first initialise social as an object since there are many social media platforms we are collecting that fit in this object
  
      profileFields.social = {};
      if (req.body.youtube) profileFields.social.youtube = req.body.youtube;
      if (req.body.twitter) profileFields.social.twitter = req.body.twitter;
      if (req.body.facebook) profileFields.social.facebook = req.body.facebook;
      if (req.body.linkedin) profileFields.social.ylinkedin = req.body.linkedin;
      if (req.body.instagram) profileFields.social.instagram = req.body.instagram;
  
      //we now need to find the user
  
      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));
        } else {
          //create the profile. but you need to first confirm if the handle exists
  
          Profile.findOne({ handle: profileFields.handle }).then(profile => {
            if (profile) {
              errors.handle = "handle already exists";
              res.status(400).json(errors);
            }
            //save the profile if non of those conditions is met
            new Profile(profileFields).save().then(profile => res.json(profile));
          });
        }
      });
    }
  );
  
  //@route  post api/experience
  //@desc   add experience to profile
  //@access   private
  
  router.post(
    "/experience",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const { errors, isValid } = validateExperienceInput(req.body);
  
      //check validation
      if (!isValid) {
        //return any errors with 400 status
        return res.status(400).json(errors);
      }
  
      Profile.findOne({ user: req.user.id }).then(profile => {
        const newExp = {
          title: req.body.title,
          company: req.body.company,
          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));
      });
    }
  );
  
  //@route  post api/education
  //@desc   add education to profile
  //@access   private
  
  router.post(
    "/education",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
      const { errors, isValid } = validateEducationInput(req.body);
  
      //check validation
      if (!isValid) {
        //return any errors with 400 status
        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,
          description: req.body.description
        };
  
        //add to experience array
  
        profile.education.unshift(newEdu);
        profile.save().then(profile => res.json(profile));
      });
    }
  );
  
  //@route  DELETE api/experience/:exp_id
  //@desc   delete experience from profile
  //@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 out of array
          profile.experience.splice(removeIndex, 1);
  
          //save
          profile.save().then(profile => res.json(profile));
        })
        .catch(err => res.status(404).json(err));
    }
  );
  
  //@route  DELETE api/education/:edu_id
  //@desc   delete education from profile
  //@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.exp_id);
  
          //splice out of array
          profile.education.splice(removeIndex, 1);
  
          //save
          profile.save().then(profile => res.json(profile));
        })
        .catch(err => res.status(404).json(err));
    }
  );
  
  module.exports = router;
  
  //https://profile.codersrank.io/user/sabitijoel
  