const { v4: uuidv4 } = require('uuid'); module.exports = (sequelize, DataTypes) => { const GuestSettings = sequelize.define('GuestSettings', { id: { type: DataTypes.CHAR(36), primaryKey: true, defaultValue: () => uuidv4(), allowNull: false, comment: 'UUID primary key' }, maxQuizzes: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 3, validate: { min: { args: [1], msg: 'Maximum quizzes must be at least 1' }, max: { args: [50], msg: 'Maximum quizzes cannot exceed 50' } }, field: 'max_quizzes', comment: 'Maximum number of quizzes a guest can take' }, expiryHours: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 24, validate: { min: { args: [1], msg: 'Expiry hours must be at least 1' }, max: { args: [168], msg: 'Expiry hours cannot exceed 168 (7 days)' } }, field: 'expiry_hours', comment: 'Guest session expiry time in hours' }, publicCategories: { type: DataTypes.JSON, allowNull: false, defaultValue: [], get() { const value = this.getDataValue('publicCategories'); if (typeof value === 'string') { try { return JSON.parse(value); } catch (e) { return []; } } return value || []; }, set(value) { this.setDataValue('publicCategories', JSON.stringify(value)); }, field: 'public_categories', comment: 'Array of category UUIDs accessible to guests' }, featureRestrictions: { type: DataTypes.JSON, allowNull: false, defaultValue: { allowBookmarks: false, allowReview: true, allowPracticeMode: true, allowTimedMode: false, allowExamMode: false }, get() { const value = this.getDataValue('featureRestrictions'); if (typeof value === 'string') { try { return JSON.parse(value); } catch (e) { return { allowBookmarks: false, allowReview: true, allowPracticeMode: true, allowTimedMode: false, allowExamMode: false }; } } return value || { allowBookmarks: false, allowReview: true, allowPracticeMode: true, allowTimedMode: false, allowExamMode: false }; }, set(value) { this.setDataValue('featureRestrictions', JSON.stringify(value)); }, field: 'feature_restrictions', comment: 'Feature restrictions for guest users' } }, { tableName: 'guest_settings', timestamps: true, underscored: true, comment: 'System-wide guest user settings' }); return GuestSettings; };