module.exports = function(sequelize, DataTypes) {
  const vorta_social_posts = sequelize.define(
    'vorta_social_posts',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      user: {
        type: DataTypes.STRING(255),
        allowNull: false,
      },
      content: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      created_at: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: false,
      freezeTableName: true,
    },
  );

  vorta_social_posts.associate = (db) => {
    db.vorta_social_posts.hasMany(db.vorta_social_comments, {
      as: 'comments',
      foreignKey: {
        name: 'post_id',
      },
      constraints: false,
    });

    db.vorta_social_posts.belongsTo(db.users, {
      as: 'account',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.vorta_social_posts.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });
  };

  return vorta_social_posts;
};
