module.exports = function(sequelize, DataTypes) {
  const sessions = sequelize.define(
    "sessions",
    {
      id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
      title: { type: DataTypes.TEXT },
      session_at: { type: DataTypes.DATE },
      status: { type: DataTypes.ENUM("planned", "completed", "draft_review", "shared"), defaultValue: "completed" },
      transcript_notes: { type: DataTypes.TEXT },
      ai_summary: { type: DataTypes.TEXT },
      key_topics: { type: DataTypes.TEXT },
      goals_discussed: { type: DataTypes.TEXT },
      blockers: { type: DataTypes.TEXT },
      commitments: { type: DataTypes.TEXT },
      homework: { type: DataTypes.TEXT },
      emotional_themes: { type: DataTypes.TEXT },
      important_quotes: { type: DataTypes.TEXT },
      follow_up_email: { type: DataTypes.TEXT },
      next_session_prep: { type: DataTypes.TEXT },
      private_coach_notes: { type: DataTypes.TEXT },
      shared_client_notes: { type: DataTypes.TEXT },
      audio_url: { type: DataTypes.TEXT },
      audio_filename: { type: DataTypes.TEXT },
      audio_mime_type: { type: DataTypes.TEXT },
      audio_size: { type: DataTypes.INTEGER },
      importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  sessions.associate = (db) => {
    db.sessions.belongsTo(db.clients, { as: "client", foreignKey: { name: "clientId" }, constraints: false });
    db.sessions.hasMany(db.action_items, { as: "action_items", foreignKey: { name: "sessionId" }, constraints: false });
    db.sessions.hasMany(db.prep_briefs, { as: "prep_briefs", foreignKey: { name: "sessionId" }, constraints: false });
    db.sessions.belongsTo(db.users, { as: "createdBy", constraints: false });
    db.sessions.belongsTo(db.users, { as: "updatedBy", constraints: false });
  };

  return sessions;
};
