const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

/**
 * Invitation Model
 * Represents invitation relationships and rewards
 */
const Invitation = sequelize.define('Invitation', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  inviterId: {
    type: DataTypes.UUID,
    allowNull: false,
    field: 'inviter_id',
    references: {
      model: 'user',
      key: 'id',
    },
  },
  inviteeId: {
    type: DataTypes.UUID,
    allowNull: false,
    field: 'invitee_id',
    references: {
      model: 'user',
      key: 'id',
    },
  },
  invitationCode: {
    type: DataTypes.STRING(20),
    allowNull: false,
    field: 'invitation_code',
  },
  registeredAt: {
    type: DataTypes.DATE,
    allowNull: false,
    field: 'registered_at',
  },
  firstPaymentAt: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'first_payment_at',
  },
  firstPaymentAmount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: true,
    field: 'first_payment_amount',
  },
  rewardAmount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: true,
    field: 'reward_amount',
  },
  rewardStatus: {
    type: DataTypes.ENUM('pending', 'credited', 'cancelled'),
    defaultValue: 'pending',
    allowNull: false,
    field: 'reward_status',
  },
}, {
  tableName: 'invitation',
  timestamps: true,
  underscored: true,
  indexes: [
    { fields: ['inviter_id'] },
    { fields: ['invitee_id'] },
    { fields: ['invitation_code'] },
    { fields: ['reward_status'] },
  ],
});

module.exports = Invitation;
