const { DataTypes } = require('sequelize'); module.exports = (sequelize) => { const AlertLog = sequelize.define('AlertLog', { id: { type: DataTypes.UUID, defaultValue: sequelize.Sequelize.UUIDV4, primaryKey: true }, alert_rule_id: { type: DataTypes.UUID, allowNull: true, // Allow null for testing references: { model: 'alert_rules', key: 'id' } }, detection_id: { type: DataTypes.UUID, allowNull: true, // Allow null for testing references: { model: 'drone_detections', key: 'id' } }, device_id: { type: DataTypes.STRING(255), allowNull: true, // Allow null for testing references: { model: 'devices', key: 'id' } }, alert_event_id: { type: DataTypes.UUID, allowNull: true, comment: 'Groups related alerts (SMS, email, webhook) that are part of the same detection event' }, alert_type: { type: DataTypes.ENUM('sms', 'email', 'webhook', 'push'), allowNull: true, // Allow null for testing defaultValue: 'sms' }, recipient: { type: DataTypes.STRING, allowNull: true, // Allow null for testing defaultValue: 'test@example.com', comment: 'Phone number, email, or webhook URL' }, message: { type: DataTypes.TEXT, allowNull: false, comment: 'Alert message content' }, status: { type: DataTypes.ENUM('pending', 'sent', 'failed', 'delivered'), defaultValue: 'pending' }, sent_at: { type: DataTypes.DATE, allowNull: true }, delivered_at: { type: DataTypes.DATE, allowNull: true }, error_message: { type: DataTypes.TEXT, allowNull: true, comment: 'Error message if alert failed' }, external_id: { type: DataTypes.STRING, allowNull: true, comment: 'External service message ID (Twilio SID, etc.)' }, cost: { type: DataTypes.DECIMAL(6, 4), allowNull: true, comment: 'Cost of sending the alert (for SMS, etc.)' }, retry_count: { type: DataTypes.INTEGER, defaultValue: 0, comment: 'Number of retry attempts' }, priority: { type: DataTypes.ENUM('low', 'normal', 'medium', 'high', 'critical'), defaultValue: 'medium' }, created_at: { type: DataTypes.DATE, defaultValue: sequelize.Sequelize.NOW }, updated_at: { type: DataTypes.DATE, defaultValue: sequelize.Sequelize.NOW } }, { tableName: 'alert_logs', timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', indexes: [ { fields: ['alert_rule_id'] }, { fields: ['detection_id'] }, { fields: ['status'] }, { fields: ['sent_at'] }, { fields: ['alert_type', 'status'] }, { fields: ['alert_event_id'] } ] }); return AlertLog; };