'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Customer extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      //    Customer.hasMany(models.Order, {
      //   foreignKey: 'customerId',
      //   as: 'orders'
      // });

      Customer.belongsTo(models.User, {
        foreignKey: 'userId',
        as: 'user'
      });

      // Customer payments
      Customer.hasMany(models.Transaction, {
        foreignKey: 'customerId',
        as: 'transactions'
      });

      // Loyalty transactions
      Customer.hasMany(models.LoyaltyPoints, {
        foreignKey: 'customerId',
        as: 'loyaltypoints'
      });

      // Returns
      Customer.hasMany(models.Return, {
        foreignKey: 'customerId',
        as: 'returns'
      });
      Customer.hasMany(models.CustomerAddress, {
        foreignKey: 'customerId',
        as: 'addresses'
      });
    }
  }
  Customer.init({
    firstName: DataTypes.STRING,
    userId: DataTypes.INTEGER,
    lastName: DataTypes.STRING,
    email: DataTypes.STRING,
    phone: DataTypes.STRING,
    address: DataTypes.TEXT,
    city: DataTypes.STRING,
    state: DataTypes.STRING,
    country: DataTypes.STRING,
    postalCode: DataTypes.STRING,
    dateOfBirth: DataTypes.DATE,
    loyaltyPoints: DataTypes.INTEGER,
    totalSpent: DataTypes.DECIMAL,
    lastPurchaseDate: DataTypes.DATE,
    isActive: DataTypes.BOOLEAN
  }, {
    sequelize,
    modelName: 'Customer',
  });
  return Customer;
};