import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import Club from './Club.js';
import { encryptData, decryptData } from '../utils/encrypt.js';

const MemberTransferConfig = sequelize.define('MemberTransferConfig', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  clubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    unique: true,
    references: {
      model: Club,
      key: 'id'
    },
    onDelete: 'CASCADE'
  },
  server: {
    type: DataTypes.STRING,
    allowNull: true,
    field: 'server',
    comment: 'Base URL des Servers (z.B. https://example.com)'
  },
  loginEndpoint: {
    type: DataTypes.STRING,
    allowNull: true,
    field: 'login_endpoint',
    comment: 'Relativer Pfad zum Login-Endpoint (z.B. /api/auth/login)'
  },
  loginFormat: {
    type: DataTypes.STRING,
    allowNull: true,
    defaultValue: 'json',
    field: 'login_format'
  },
  encryptedLoginCredentials: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'encrypted_login_credentials',
    comment: 'Verschlüsselte Login-Daten als JSON-String'
  },
  transferEndpoint: {
    type: DataTypes.STRING,
    allowNull: false,
    field: 'transfer_endpoint',
    comment: 'Relativer Pfad zum Übertragungs-Endpoint (z.B. /api/members/bulk)'
  },
  transferMethod: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: 'POST',
    field: 'transfer_method'
  },
  transferFormat: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: 'json',
    field: 'transfer_format'
  },
  transferTemplate: {
    type: DataTypes.TEXT,
    allowNull: false,
    field: 'transfer_template'
  },
  useBulkMode: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
    field: 'use_bulk_mode'
  },
  bulkWrapperTemplate: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'bulk_wrapper_template',
    comment: 'Optionales Template für die äußere Struktur im Bulk-Modus (z.B. {"data": {"members": "{{members}}"}})'
  }
}, {
  underscored: true,
  tableName: 'member_transfer_config',
  timestamps: true
});

// Getter/Setter für verschlüsselte Login-Daten
MemberTransferConfig.prototype.getLoginCredentials = function() {
  if (!this.encryptedLoginCredentials) {
    return null;
  }
  try {
    const decrypted = decryptData(this.encryptedLoginCredentials);
    return JSON.parse(decrypted);
  } catch (error) {
    console.error('[MemberTransferConfig] Error decrypting login credentials:', error);
    return null;
  }
};

MemberTransferConfig.prototype.setLoginCredentials = function(credentials) {
  if (!credentials || Object.keys(credentials).length === 0) {
    this.encryptedLoginCredentials = null;
    return;
  }
  try {
    const jsonString = JSON.stringify(credentials);
    this.encryptedLoginCredentials = encryptData(jsonString);
  } catch (error) {
    console.error('[MemberTransferConfig] Error encrypting login credentials:', error);
    this.encryptedLoginCredentials = null;
  }
};

export default MemberTransferConfig;

