const UserModel = require('../../../models/UserModel')
const jwt = require('jsonwebtoken')


// GET user by ID from URL
const getUserById = async (req, res, next) => {
  try {
    const userId = req.params.id; // گرفتن آیدی از پارامتر URL
    const service = req.query.services;

    const user = await UserModel.findById(userId);
    if (!user) {
      return res.status(404).json({ message: "کاربر مورد نظر یافت نشد" });
    }

    let isRegister = true;
    // if (
    //   !user.user_name ||
    //   !user.first_name ||
    //   !user.last_name ||
    //   !user.user_type ||
    //   !user.national_card_image ||
    //   !user.password ||
    //   !user.national_code
    // ) {
    //   isRegister = false;
    // }

    const response = {
      _id: user._id,
      user_type: user.user_type,
      user_level: user.user_level,
      first_name: user.first_name,
      last_name: user.last_name,
      user_name: user.user_name,
      show_location: user.show_location,
      is_verified: user.is_verified,
      user_score: user.user_score,
      rate: user.rate,
      bio: user.bio,
      height: user.height,
      weight: user.weight,
      size: user.size,
      eye_color: user.eye_color,
      hair_color: user.hair_color,
      profile_image: user.profile_image,
      lat: user.lat,
      lng: user.lng,
      address: user.address,
      province: user.province,
      city: user.city,
      shaba: user.shaba,
      national_code: user.national_code,
      mobile: user.mobile,
      cooperation_abroad: user.cooperation_abroad,
      cooperation_type: user.cooperation_type,
      conversation_projects: user.conversation_projects,
      expertise: user.expertise,
      sub_expertise: user.sub_expertise,
      isRegister,
      user,
      services: service === "1" ? user.services : null
    };

    return res.status(200).json({ user: response });
  } catch (error) {
    next(error);
  }
};

const getProfile = async (req, res, next) => {
  try {
    const token = req.header('Authorization').split(' ')[1]
    if (!token) return res.status(401).send('Access Denied')
    const service = req.query.services

    // eslint-disable-next-line no-unused-vars
    const decodedToken = jwt.verify(token, process.env.APP_SECRET)
    const userId = decodedToken.id
    const user = await UserModel.findById(userId)
    let isRegister = true
    if (!user.user_name ||
    !user.first_name ||
    !user.last_name ||
    !user.user_type ||
    !user.national_card_image ||
    !user.password ||
    !user.national_code
    ) {
      isRegister = false
    }
    if (!user) {
      return res.status(404).json({ message: 'کاربر مورد نظر یافت نشد' })
    }
    const response = {
      _id: user._id,
      user_type: user.user_type,
      user_level: user.user_level,
      first_name: user.first_name,
      last_name: user.last_name,
      user_name: user.user_name,
      show_location: user.show_location,
      is_verified: user.is_verified,
      is_Register: user.is_Register,
      user_score: user.user_score,
      rate: user.rate,
      bio: user.bio,
      height: user.height,
      weight: user.weight,
      size: user.size,
      eye_color: user.eye_color,
      hair_color: user.hair_color,
      profile_image: user.profile_image,
      lat: user.lat,
      lng: user.lng,
      address: user.address,
      province: user.province,
      city: user.city,
      shaba: user.shaba,
      national_code: user.national_code,
      mobile: user.mobile,
      cooperation_abroad: user.cooperation_abroad,
      cooperation_type: user.cooperation_type,
      conversation_projects: user.conversation_projects,
      expertise: user.expertise,
      sub_expertise: user.sub_expertise,
      isRegister,
      services: service === '1' ? user.services : null
    }
    return res.status(200).json({ user: response })
  } catch (error) {
    next(error)
  }
}

module.exports = {
  getProfile,
  getUserById
};
