module.exports = function (sequelize, DataTypes) {
  const pwa_caches = sequelize.define(
    'pwa_caches',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

      environment: {
        type: DataTypes.ENUM,

        values: ['dev', 'stage', 'production'],
      },

      cache_version: {
        type: DataTypes.TEXT,
        validate: {
          len: {
            args: [0, 255],
            msg: 'Cache version must be at most 255 characters',
          },
        },
      },

      manifest_json: {
        type: DataTypes.JSON,
      },

      asset_list_json: {
        type: DataTypes.JSON,
      },

      generated_at: {
        type: DataTypes.DATE,
      },

      is_active: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  pwa_caches.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    //end loop

    db.pwa_caches.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.pwa_caches.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.pwa_caches.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };

  return pwa_caches;
};
