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

const MemberNote = sequelize.define('MemberNote', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
    },
    content: {
        type: DataTypes.TEXT,
        allowNull: false,
        set(value) {
            const encryptedValue = encryptData(value);
            this.setDataValue('content', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('content');
            return decryptData(encryptedValue);
        }
    },
    memberId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
            model: Member,
            key: 'id',
        },
        onDelete: 'CASCADE',
    },
}, {
    underscored: true,
    tableName: 'member_notes',
    timestamps: true,
});

export default MemberNote;
