module.exports = (sequelize, DataTypes) => {
  const UserDiscounts = sequelize.define(
    'UserDiscounts',
    {
      id: {
        allowNull: false,
        autoIncrement: false,
        primaryKey: true,
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
      },
      customer_id: {
        type: DataTypes.UUID,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id',
        },
      },
      discount_id: {
        type: DataTypes.UUID,
        allowNull: true,
      },
      listing_discount_id: {
        type: DataTypes.UUID,
        allowNull: true,
      },
      status: {
        type: DataTypes.ENUM('active', 'inactive', 'invalid', 'expired', 'used'),
        allowNull: false,
        defaultValue: 'active',
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
      },
      updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
      },
    },
    {
      tableName: 'user_discounts',
      timestamps: true,
    },
  );

  UserDiscounts.associate = (models) => {
    UserDiscounts.belongsTo(models.Discounts, {
      as: 'global_discount',
      foreignKey: 'discount_id',
    });

    UserDiscounts.belongsTo(models.ListingDiscounts, {
      as: 'listing_discount',
      foreignKey: 'listing_discount_id',
    });
  };

  return UserDiscounts;
};
