const { DataTypes } = require('sequelize'); module.exports = (sequelize) => { const Heartbeat = sequelize.define('Heartbeat', { id: { type: DataTypes.UUID, defaultValue: sequelize.Sequelize.UUIDV4, primaryKey: true }, device_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'devices', key: 'id' }, comment: 'ID of the device sending heartbeat' }, device_key: { type: DataTypes.STRING, allowNull: true, // Allow null for testing defaultValue: 'test-device-key', comment: 'Unique key of the sensor from heartbeat message' }, signal_strength: { type: DataTypes.INTEGER, allowNull: true, comment: 'Signal strength at time of heartbeat' }, battery_level: { type: DataTypes.INTEGER, allowNull: true, comment: 'Battery level percentage (0-100)' }, temperature: { type: DataTypes.DECIMAL(4, 1), allowNull: true, comment: 'Device temperature in Celsius' }, uptime: { type: DataTypes.BIGINT, allowNull: true, comment: 'Device uptime in seconds' }, memory_usage: { type: DataTypes.INTEGER, allowNull: true, comment: 'Memory usage percentage' }, firmware_version: { type: DataTypes.STRING, allowNull: true, comment: 'Firmware version reported in heartbeat' }, received_at: { type: DataTypes.DATE, defaultValue: sequelize.Sequelize.NOW, comment: 'When heartbeat was received by server' }, raw_payload: { type: DataTypes.JSON, allowNull: true, comment: 'Complete raw payload received from detector (for debugging)' }, created_at: { type: DataTypes.DATE, defaultValue: sequelize.Sequelize.NOW } }, { tableName: 'heartbeats', timestamps: true, createdAt: 'created_at', updatedAt: false, indexes: [ { fields: ['device_id'] }, { fields: ['received_at'] }, { fields: ['device_id', 'received_at'] } ] }); return Heartbeat; };