module.exports = function(sequelize, DataTypes) {
  const resources = sequelize.define(
    "resources",
    {
      id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
      title: { type: DataTypes.TEXT },
      description: { type: DataTypes.TEXT },
      url: { type: DataTypes.TEXT },
      resource_type: { type: DataTypes.ENUM("link", "worksheet", "pdf", "video"), defaultValue: "link" },
      is_shared: { type: DataTypes.BOOLEAN, defaultValue: true },
      importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

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

  return resources;
};
