const Member = require('../models/member');
const bcrypt = require('bcryptjs');
const saltRounds = 10; 

exports.getUsers = async (req, res) => {
  try {
    const members = await Member.findAll();
    res.status(200).json(members);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
};

exports.getUsersByID = async (req, res) => {
  try {
    const { id } = req.params;
    const member = await Member.findByPk(id);
    if (member) {
        res.status(200).json({ actionStatus: 'Success', data: member });
    } else {
        res.status(404).json({ actionStatus: 'Failed', message: 'Product not found' });
    }
} catch (error) {
    res.status(500).json({ actionStatus: 'Failed', message: error.message });
}
};


exports.createUser = async (req, res) => {
  try {
    const { member_username, member_password, member_firstname, member_lastname, member_date, member_phone, member_address, member_gender, bio, email,member_position } = req.body;

  
    const hashedPassword = await bcrypt.hash(member_password, saltRounds);

    const newMember = await Member.create({
      member_username,
      member_password: hashedPassword,
      member_firstname,
      member_lastname,
      member_date,
      member_phone,
      member_address,
      member_gender,
      member_position,
      bio,
      email
    });

    res.status(201).json({
      actionStatus: 'Success',
      message: 'Create Success!!',
      statusCode: 200,
      member: newMember
    });
  } catch (err) {
    res.status(400).json({ error: err.message });
  }
};


exports.updateUser = async (req, res) => {
  try {
    const { id } = req.params;
    const { member_username, member_password, member_firstname, member_lastname, member_date, member_phone, member_address, member_gender, bio, email,member_position } = req.body;
    
    const member = await Member.findByPk(id);

    if (member) {
      
      member.member_username = member_username || member.member_username;
      if (member_password) {
        const hashedPassword = await bcrypt.hash(member_password, saltRounds);
        member.member_password = hashedPassword;
      }
      member.member_firstname = member_firstname || member.member_firstname;
      member.member_lastname = member_lastname || member.member_lastname;
      member.member_date = member_date || member.member_date;
      member.member_phone = member_phone || member.member_phone;
      member.member_address = member_address || member.member_address;
      member.member_gender = member_gender || member.member_gender;
      member.member_position = member_position || member_position;
      member.bio = bio || member.bio;
      member.email = email || member.email;

      
      await member.save();

      res.status(200).json({
        actionStatus: 'Success',
        message: 'Update Success!!',
        statusCode: 200,
        member: member
      });
    } else {
      res.status(404).json({ error: 'Member not found' });
    }
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
};


exports.deleteUser = async (req, res) => {
  try {
    const { id } = req.params;

    
    const member = await Member.findByPk(id);

    if (member) {
      
      await member.destroy();
      res.status(200).json({
        actionStatus: 'Success',
        message: 'Delete Success!!',
        statusCode: 200
      }); 
    } else {
      res.status(404).json({ error: 'Member not found' });
    }
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
};
