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

      environment: {
        type: DataTypes.ENUM,
        allowNull: false,

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

      path: {
        type: DataTypes.TEXT,
        validate: {
          len: { args: [0, 2048], msg: 'Path must be at most 2048 characters' },
        },
      },

      ip_address: {
        type: DataTypes.TEXT,
        validate: {
          len: {
            args: [0, 45],
            msg: 'IP address must be at most 45 characters',
          },
        },
      },

      user_agent: {
        type: DataTypes.TEXT,
        validate: {
          len: {
            args: [0, 1024],
            msg: 'User agent must be at most 1024 characters',
          },
        },
      },

      accessed_at: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
      indexes: [
        { fields: ['projectId'] },
        { fields: ['environment'] },
        { fields: ['userId'] },
        { fields: ['accessed_at'] },
      ],
    },
  );

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

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

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

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

  return access_logs;
};
