const { Model, DataTypes } = require('sequelize');

/**
 * AuthInfo Model
 * Repräsentiert die auth_info-Tabelle
 */
class AuthInfo extends Model {
  static initialize(sequelize) {
    AuthInfo.init(
      {
        id: {
          type: DataTypes.BIGINT,
          primaryKey: true,
          autoIncrement: true,
          allowNull: false
        },
        version: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: 0
        },
        user_id: {
          type: DataTypes.BIGINT,
          allowNull: true,
          references: {
            model: 'user',
            key: 'id'
          }
        },
        password_hash: {
          type: DataTypes.STRING(100),
          allowNull: false
        },
        password_method: {
          type: DataTypes.STRING(20),
          allowNull: false
        },
        password_salt: {
          type: DataTypes.STRING(60),
          allowNull: false
        },
        status: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: 0
        },
        failed_login_attempts: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: 0
        },
        last_login_attempt: {
          type: DataTypes.DATE,
          allowNull: true
        },
        email: {
          type: DataTypes.STRING(256),
          allowNull: false,
          unique: true
        },
        unverified_email: {
          type: DataTypes.STRING(256),
          allowNull: false,
          defaultValue: ''
        },
        email_token: {
          type: DataTypes.STRING(64),
          allowNull: false,
          defaultValue: ''
        },
        email_token_expires: {
          type: DataTypes.DATE,
          allowNull: true
        },
        email_token_role: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: 0
        }
      },
      {
        sequelize,
        tableName: 'auth_info',
        timestamps: false,
        indexes: [
          {
            name: 'fk_auth_info_user',
            fields: ['user_id']
          }
        ]
      }
    );

    return AuthInfo;
  }
}

module.exports = AuthInfo;

