import { Model, DataTypes, Sequelize } from 'sequelize';

export interface NotificationAttributes {
    id: string;
    userId: string;
    title: string;
    message: string;
    type: string;
    link: string | null;
    isRead: boolean;
}

export interface NotificationInstance extends Model<NotificationAttributes>, NotificationAttributes { }

export default (sequelize: Sequelize) => {
    const Notification = sequelize.define<NotificationInstance>('Notification', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        userId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        title: {
            type: DataTypes.STRING,
            allowNull: false
        },
        message: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        type: {
            type: DataTypes.STRING, // info, warning, success, error
            defaultValue: 'info'
        },
        link: {
            type: DataTypes.STRING,
            allowNull: true
        },
        isRead: {
            type: DataTypes.BOOLEAN,
            defaultValue: false
        }
    }, {
        tableName: 'notifications',
        timestamps: true,
        indexes: [
            { fields: ['userId'] },
            { fields: ['isRead'] }
        ]
    });

    (Notification as any).associate = (models: any) => {
        Notification.belongsTo(models.User, {
            foreignKey: 'userId',
            as: 'recipient'
        });
    };

    return Notification;
};
