import { DataTypes } from 'sequelize';
import sequelize from '../database.js';

const ClubCommunicationRecipient = sequelize.define('ClubCommunicationRecipient', {
  id: {
    type: DataTypes.BIGINT,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false,
  },
  threadId: {
    type: DataTypes.BIGINT,
    allowNull: false,
    field: 'thread_id',
  },
  clubId: {
    type: DataTypes.BIGINT,
    allowNull: false,
    field: 'club_id',
  },
  memberId: {
    type: DataTypes.BIGINT,
    allowNull: true,
    field: 'member_id',
  },
  recipientName: {
    type: DataTypes.STRING(255),
    allowNull: false,
    field: 'recipient_name',
  },
  emailSnapshot: {
    type: DataTypes.STRING(255),
    allowNull: true,
    field: 'email_snapshot',
  },
  deliveryStatus: {
    type: DataTypes.ENUM('pending', 'sent', 'failed', 'skipped'),
    allowNull: false,
    defaultValue: 'pending',
    field: 'delivery_status',
  },
  deliveredAt: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'delivered_at',
  },
  lastAttemptAt: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'last_attempt_at',
  },
  attemptCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'attempt_count',
  },
  retryable: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  errorCode: {
    type: DataTypes.STRING(64),
    allowNull: true,
    field: 'error_code',
  },
  transportMessageId: {
    type: DataTypes.STRING(255),
    allowNull: true,
    field: 'transport_message_id',
  },
  errorMessage: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'error_message',
  },
}, {
  underscored: true,
  tableName: 'club_communication_recipients',
  timestamps: true,
});

export default ClubCommunicationRecipient;
