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

const DiaryMemberNote = sequelize.define('DiaryMemberNote', {
    memberId: {
        type: DataTypes.INTEGER,
        references: {
            model: Member,
            key: 'id',
        },
        onDelete: 'CASCADE',
        allowNull: false,
    },
    diaryDateId: {
        type: DataTypes.INTEGER,
        references: {
            model: DiaryDate,
            key: 'id',
        },
        onDelete: 'CASCADE',
        allowNull: false,
    },
    content: {
        type: DataTypes.STRING(4096),
        allowNull: false,
        set(value) {
            const encryptedValue = encryptData(value);
            this.setDataValue('content', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('content');
            return decryptData(encryptedValue);
        }
    },
}, {
    underscored: true,
    tableName: 'diary_member_notes',
    timestamps: true,
});

export default DiaryMemberNote;
