text: const express = require("express");
  const request = require("request");
  const config = require("config");
  const router = express.Router();
  const auth = require("../../middleware/auth");
  const { check, validationResult } = require("express-validator/check");
  
  const Profile = require("../../models/Profile");
  const User = require("../../models/User");
  const Post = require("../../models/Post");
  
  // @route    GET api/profile/me
  // @desc     Get current users profile
  // @access   Private
  router.get("/me", auth, async (req, res) => {
    try {
      const profile = await Profile.findOne({
        user: req.user.id
      }).populate("user", ["name"]);
  
      if (!profile) {
        return res.status(400).json({ msg: "There is no profile for this user" });
      }
  
      res.json(profile);
    } catch (err) {
      console.error(err.message);
      res.status(500).send("Server Error");
    }
  });
  
  // @route    POST api/profile
  // @desc     Create, update user profile
  // @access   Private
  router.post(
    "/",
    [
      auth,
      [
        check("status", "Status is required")
          .not()
          .isEmpty()
        // check("instruments", "instruments is required")
        //   .not()
        //   .isEmpty()
      ]
    ],
    async (req, res) => {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
      }
  
      const {
        bandName,
        website,
        location,
        bio,
        status,
        instruments,
        youtube,
        facebook,
        twitter,
        instagram,
        linkedin
      } = req.body;
  
      // Build profile object
      const profileFields = {};
      (profileFields.user = req.user.id, req.user.name);
      if (bandName) profileFields.bandName = bandName;
      if (website) profileFields.website = website;
      if (location) profileFields.location = location;
      if (bio) profileFields.bio = bio;
      if (status) profileFields.status = status;
      if (instruments) {
        profileFields.instruments = instruments
          .split(",")
          .map(instruments => instruments.trim());
      }
  
      // Build social object
      profileFields.social = {};
      if (youtube) profileFields.social.youtube = youtube;
      if (twitter) profileFields.social.twitter = twitter;
      if (facebook) profileFields.social.facebook = facebook;
      if (linkedin) profileFields.social.linkedin = linkedin;
      if (instagram) profileFields.social.instagram = instagram;
  
      try {
        // Using upsert option (creates new doc if no match is found):
        let profile = await Profile.findOneAndUpdate(
          { user: req.user.id },
          { $set: profileFields },
          { new: true, upsert: true }
        );
        res.json(profile);
      } catch (err) {
        console.error(err.message);
        res.status(500).send("Server Error");
      }
    }
  );
  
  // @route    GET api/profile
  // @desc     Get all profiles
  // @access   Public
  router.get("/", async (req, res) => {
    try {
      const profiles = await Profile.find().populate("user", ["name"]);
      res.json(profiles);
    } catch (err) {
      console.error(err.message);
      res.status(500).send("Server Error");
    }
  });
  
  // @route    GET api/profile/user/:user_id
  // @desc     Get profile by user ID
  // @access   Public
  router.get("/user/:user_id", async (req, res) => {
    try {
      const profile = await Profile.findOne({
        user: req.params.user_id
      }).populate("user", ["name"]);
  
      if (!profile) return res.status(400).json({ msg: "Profile not found" });
  
      res.json(profile);
    } catch (err) {
      console.error(err.message);
      if (err.kind == "ObjectId") {
        return res.status(400).json({ msg: "Profile not found" });
      }
      res.status(500).send("Server Error");
    }
  });
  
  // @route    DELETE api/profile
  // @desc     Delete profile, user & posts
  // @access   Private
  router.delete("/", auth, async (req, res) => {
    try {
      // Remove user posts
      await Post.deleteMany({ user: req.user.id });
      // Remove profile
      await Profile.findOneAndRemove({ user: req.user.id });
      // Remove user
      await User.findOneAndRemove({ _id: req.user.id });
  
      res.json({ msg: "User deleted" });
    } catch (err) {
      console.error(err.message);
      res.status(500).send("Server Error");
    }
  });
  
  // @route    PUT api/profile/experience
  // @desc     Add profile experience
  // @access   Private
  router.put(
    "/experience",
    [
      auth,
      [
        check("title", "Title is required")
          .not()
          .isEmpty(),
        check("bandName", "Band or Act Name is required")
          .not()
          .isEmpty(),
        check("from", "From date is required")
          .not()
          .isEmpty()
      ]
    ],
    async (req, res) => {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
      }
  
      const {
        bandName,
        title,
        genre,
        instruments,
        location,
        from,
        to,
        current,
        description
      } = req.body;
  
      const newExp = {
        bandName,
        title,
        genre,
        instruments,
        location,
        from,
        to,
        current,
        description
      };
  
      try {
        const profile = await Profile.findOne({ user: req.user.id });
  
        profile.experience.unshift(newExp);
  
        await profile.save();
  
        res.json(profile);
      } catch (err) {
        console.error(err.message);
        res.status(500).send("Server Error");
      }
    }
  );
  
  // @route    DELETE api/profile/experience/:exp_id
  // @desc     Delete experience from profile
  // @access   Private
  // router.delete('/experience/:exp_id', auth, async (req, res) => {
  //   try {
  //     const profile = await Profile.findOne({ user: req.user.id });
  
  //     // Get remove index
  //     const removeIndex = profile.experience
  //       .map(item => item.id)
  //       .indexOf(req.params.exp_id);
  
  //     profile.experience.splice(removeIndex, 1);
  
  //     await profile.save();
  
  //     res.json(profile);
  //   } catch (err) {
  //     console.error(err.message);
  //     res.status(500).send('Server Error');
  //   }
  // });
  
  router.delete("/experience/:exp_id", auth, async (req, res) => {
    try {
      const foundProfile = await Profile.findOne({ user: req.user.id });
      const expIds = foundProfile.experience.map(exp => exp._id.toString());
      // if i dont add .toString() it returns this weird mongoose coreArray and the ids are somehow objects and it still deletes anyway even if you put /experience/5
      const removeIndex = expIds.indexOf(req.params.exp_id);
      if (removeIndex === -1) {
        return res.status(500).json({ msg: "Server error" });
      } else {
        console.log("expIds", expIds);
        console.log("typeof expIds", typeof expIds);
        console.log("req.params", req.params);
        console.log("removed", expIds.indexOf(req.params.exp_id));
        foundProfile.experience.splice(removeIndex, 1);
        await foundProfile.save();
        return res.status(200).json(foundProfile);
      }
    } catch (error) {
      console.error(error);
      return res.status(500).json({ msg: "Server error" });
    }
  });
  
  // @route    PUT api/profile/education
  // @desc     Add profile education
  // @access   Private
  router.put(
    "/education",
    [
      auth,
      [
        check("school", "School is required")
          .not()
          .isEmpty(),
        check("degree", "Degree is required")
          .not()
          .isEmpty(),
        check("fieldofstudy", "Field of study is required")
          .not()
          .isEmpty(),
        check("from", "From date is required")
          .not()
          .isEmpty()
      ]
    ],
    async (req, res) => {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
      }
  
      const {
        school,
        degree,
        fieldofstudy,
        from,
        to,
        current,
        description
      } = req.body;
  
      const newEdu = {
        school,
        degree,
        fieldofstudy,
        from,
        to,
        current,
        description
      };
  
      try {
        const profile = await Profile.findOne({ user: req.user.id });
  
        profile.education.unshift(newEdu);
  
        await profile.save();
  
        res.json(profile);
      } catch (err) {
        console.error(err.message);
        res.status(500).send("Server Error");
      }
    }
  );
  
  // @route    DELETE api/profile/education/:edu_id
  // @desc     Delete education from profile
  // @access   Private
  //router.delete('/education/:edu_id', auth, async (req, res) => {
  //try {
  //const profile = await Profile.findOne({ user: req.user.id });
  
  // Get remove index
  //const removeIndex = profile.education
  //.map(item => item.id)
  //.indexOf(req.params.edu_id);
  /*
      profile.education.splice(removeIndex, 1);
      await profile.save();
      res.json(profile);
    } catch (err) {
      console.error(err.message);
      res.status(500).send('Server Error');
    }
  });
  */
  
  router.delete("/education/:edu_id", auth, async (req, res) => {
    try {
      const foundProfile = await Profile.findOne({ user: req.user.id });
      const eduIds = foundProfile.education.map(edu => edu._id.toString());
      // if i dont add .toString() it returns this weird mongoose coreArray and the ids are somehow objects and it still deletes anyway even if you put /education/5
      const removeIndex = eduIds.indexOf(req.params.edu_id);
      if (removeIndex === -1) {
        return res.status(500).json({ msg: "Server error" });
      } else {
        // theses console logs helped me figure it out
        /*   console.log("eduIds", eduIds);
        console.log("typeof eduIds", typeof eduIds);
        console.log("req.params", req.params);
        console.log("removed", eduIds.indexOf(req.params.edu_id));
   */ foundProfile.education.splice(
          removeIndex,
          1
        );
        await foundProfile.save();
        return res.status(200).json(foundProfile);
      }
    } catch (error) {
      console.error(error);
      return res.status(500).json({ msg: "Server error" });
    }
  });
  
  module.exports = router;
  