module.exports = (sequelize, DataTypes) => {
  const Client = sequelize.define(
    "Client",
    {
      id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
      },

      name: {
        type: DataTypes.STRING(255),
        allowNull: false,
        unique: false,
      },

      email: {
        type: DataTypes.STRING(255),
        allowNull: false,
        unique: true,
        validate: {
          isEmail: true,
        },
      },

      phone: {
        type: DataTypes.STRING(14),
        allowNull: false,
        validate: {
          isNumeric: true,
        },
      },

      address: {
        type: DataTypes.STRING(255),
        allowNull: true,
      },

      notes: {
        type: DataTypes.STRING(511),
        allowNull: true,
      },

      createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: sequelize.literal("NOW()"),
      },

      updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: sequelize.literal("NOW()"),
      },
    },
    {
      tableName: "sl_clients",
    }
  );

  Client.associate = () => {
    const { ClientInterest, StaffMember } = sequelize.models;

    Client.hasMany(ClientInterest, {
      as: "interests",
      foreignKey: "clientId",
    });

    Client.belongsTo(StaffMember, {
      as: "expert",
    });
  };

  return Client;
};
