const { DataTypes } = require("sequelize");
const sequelize = require("../config/database");
const bcrypt = require("bcryptjs");
const Role = require("../models/role");
const Position = require("./position");

const Member = sequelize.define(
  "Member",
  {
    member_id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    member_firstname: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    member_lastname: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    member_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
    },
    member_phone: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    member_address: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    member_gender: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    member_position: {
      type: DataTypes.NUMBER,
      references: {
        model: Position,
        key: "id",
      },
    },
    member_username: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
    member_password: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    bio: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    // role_id: {
    //     type: DataTypes.UUID,
    //     references: {
    //         model: Role,
    //         key: 'role_id',
    //     },
    // }
  },
  {
    tableName: "member_employee",
    timestamps: false,
    underscored: true,
  }
);

// Hash password before saving
Member.beforeCreate(async (member) => {
  member.member_password = await bcrypt.hash(member.member_password, 10);
});

module.exports = Member;
