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

const Member = sequelize.define('User', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  hashedId: {
    type: DataTypes.STRING,
    unique: true,
    allowNull: false,
    defaultValue() {
      return crypto.randomBytes(64).toString('hex');
    }
  },
  firstName: {
    type: DataTypes.STRING,
    allowNull: false,
    set(value) {
      const encryptedValue = encryptData(value);
      this.setDataValue('firstName', encryptedValue);
    },
    get() {
      const encryptedValue = this.getDataValue('firstName');
      return decryptData(encryptedValue);
    }
  },
  lastName: {
    type: DataTypes.STRING,
    allowNull: false,
    set(value) {
      const encryptedValue = encryptData(value);
      this.setDataValue('lastName', encryptedValue);
    },
    get() {
      const encryptedValue = this.getDataValue('lastName');
      return decryptData(encryptedValue);
    }
  },
  birthDate: {
    type: DataTypes.STRING, 
    allowNull: false,
    set(value) {
      const encryptedValue = encryptData(value);
      this.setDataValue('birthDate', encryptedValue);
    },
    get() {
      const encryptedValue = this.getDataValue('birthDate');
      return decryptData(encryptedValue);
    }
  },
  phone: {
    type: DataTypes.STRING,
    allowNull: false,
    set(value) {
      const encryptedValue = encryptData(value);
      this.setDataValue('phone', encryptedValue);
    },
    get() {
      const encryptedValue = this.getDataValue('phone');
      return decryptData(encryptedValue);
    }
  },
  street: {
    type: DataTypes.STRING,
    allowNull: false,
    set(value) {
      const encryptedValue = encryptData(value);
      this.setDataValue('street', encryptedValue);
    },
    get() {
      const encryptedValue = this.getDataValue('street');
      return decryptData(encryptedValue);
    }
  },
  city: {
    type: DataTypes.STRING,
    allowNull: false,
    set(value) {
      const encryptedValue = encryptData(value);
      this.setDataValue('city', encryptedValue);
    },
    get() {
      const encryptedValue = this.getDataValue('city');
      return decryptData(encryptedValue);
    }
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    set(value) {
      const encryptedValue = encryptData(value);
      this.setDataValue('email', encryptedValue);
    }, 
    get() {
      const encryptedValue = this.getDataValue('email');
      return decryptData(encryptedValue);
    }
  },
  clubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
  },
  active: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    default: true,
  },
  testMembership: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    default: false,
  },
  picsInInternetAllowed: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    default: false,
  }
}, {
  underscored: true,
  sequelize,
  modelName: 'Member',
  tableName: 'member',
  timestamps: true,

  hooks: {
    afterCreate: (member) => {
      member.hashedId = crypto.createHash('sha256').update(String(member.id)).digest('hex');
      member.save();
    },
  }
},
  {
    underscored: true,
    tableName: 'log',
    timestamps: true
  }
);

Member.belongsTo(Club, { as: 'club' });
Club.hasMany(Member, { as: 'members' });

export default Member;
