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

      purpose: {
        type: DataTypes.ENUM,

        values: ['upload', 'download'],
      },

      asset_type: {
        type: DataTypes.ENUM,

        values: ['image', 'video', 'audio', 'file'],
      },

      requested_key: {
        type: DataTypes.TEXT,
        validate: {
          len: {
            args: [0, 1024],
            msg: 'Requested key must be at most 1024 characters',
          },
        },
      },

      mime_type: {
        type: DataTypes.TEXT,
        validate: {
          len: {
            args: [0, 255],
            msg: 'MIME type must be at most 255 characters',
          },
          isMimeTypeOrEmpty(value) {
            if (
              value &&
              value.length > 0 &&
              !/^[\w.-]+\/[\w.+-]+$/.test(value)
            ) {
              throw new Error('MIME type must be in format type/subtype');
            }
          },
        },
      },

      requested_size_mb: {
        type: DataTypes.DECIMAL,
        validate: {
          min: {
            args: [0],
            msg: 'Requested size must be a non-negative number',
          },
        },
      },

      expires_at: {
        type: DataTypes.DATE,
      },

      status: {
        type: DataTypes.TEXT,
      },

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

  presigned_url_requests.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.presigned_url_requests.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.presigned_url_requests.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

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

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

  return presigned_url_requests;
};
