module.exports = function(sequelize, DataTypes) {
  const clients = sequelize.define(
    "clients",
    {
      id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
      name: { type: DataTypes.TEXT },
      email: { type: DataTypes.TEXT },
      status: { type: DataTypes.ENUM("lead", "active", "paused", "completed"), defaultValue: "active" },
      goals: { type: DataTypes.TEXT },
      notes: { type: DataTypes.TEXT },
      company: { type: DataTypes.TEXT },
      role_title: { type: DataTypes.TEXT },
      tags: { type: DataTypes.TEXT },
      next_session_at: { type: DataTypes.DATE },
      last_session_at: { type: DataTypes.DATE },
      importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  clients.associate = (db) => {
    db.clients.belongsTo(db.packages, { as: "package", foreignKey: { name: "packageId" }, constraints: false });
    db.clients.belongsTo(db.users, { as: "owner", foreignKey: { name: "ownerId" }, constraints: false });
    db.clients.hasMany(db.sessions, { as: "sessions", foreignKey: { name: "clientId" }, constraints: false });
    db.clients.hasMany(db.action_items, { as: "action_items", foreignKey: { name: "clientId" }, constraints: false });
    db.clients.hasMany(db.resources, { as: "resources", foreignKey: { name: "clientId" }, constraints: false });
    db.clients.hasMany(db.prep_briefs, { as: "prep_briefs", foreignKey: { name: "clientId" }, constraints: false });
    db.clients.belongsTo(db.users, { as: "createdBy", constraints: false });
    db.clients.belongsTo(db.users, { as: "updatedBy", constraints: false });
  };

  return clients;
};
