const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const CustomerLists = sequelize.define(
    "customerlist",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      name: {
        type: DataTypes.STRING(100),
        allowNull: false,
        unique: true,
      },

      type: { type: DataTypes.STRING(100), allowNull: true },
      bank: { type: DataTypes.STRING(100), allowNull: true },
      currency: { type: DataTypes.STRING(100), allowNull: true },
      market_segment: { type: DataTypes.STRING(100), allowNull: true },
      industry: { type: DataTypes.STRING(100), allowNull: true },
      customer_contact: { type: DataTypes.STRING(100), allowNull: true },
      website: { type: DataTypes.STRING(100), allowNull: true },
      subterritory_id: { type: DataTypes.INTEGER, allowNull: true },
      import_from: { type: DataTypes.STRING(100), allowNull: true },
      ids: { type: DataTypes.INTEGER, allowNull: true },
      //   fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "companies",
          key: "id",
        },
      },

      territory_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "territories",
          key: "id",
        },
      },
    },
    {
      tableName: "customerlists",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Customer List model with other models.
   * @param {object} models - The models to associate with.
   */
  CustomerLists.associate = (models) => {
    /**
     * Associates the CustomerLists model with the AddressChild model.
     * A CustomerList has many AddressChilds.
     * @param {object} models - The models to associate with.
     */
    CustomerLists.hasMany(models.AddressChild, {
      foreignKey: "customer_id",
      as: "addresschilds",
      onDelete: "CASCADE",
    });

    /**
     * Associates the CustomerLists model with the Customer PIC Child model.
     * A CustomerList has many Customer PIC Child.
     * @param {object} models - The models to associate with.
     */
    CustomerLists.hasMany(models.CustomerPICChild, {
      foreignKey: "customer_id",
      as: "customerpicchilds",
      onDelete: "CASCADE",
    });

    /**
     * Associates the CustomerLists model with the Company model.
     * A CustomerList belongs to a Company.
     * @param {object} models - The models to associate with.
     */
    CustomerLists.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });

    /**
     * Associates the CustomerLists model with the Territory model.
     * A CustomerLists belongs to a Territory.
     * @param {object} models - The models to associate with.
     */
    CustomerLists.belongsTo(models.Territory, {
      foreignKey: "territory_id",
      as: "territory",
    });

    /**
     * Associates the CustomerLists model with the SalesPlan model.
     * A CustomerList has many SalesPlans.
     * @param {object} models - The models to associate with.
     */
    CustomerLists.hasMany(models.SalesPlanItem, {
      foreignKey: "customer_id",
      as: "salesplanitems",
    });

    /**
     * Associates the CustomerLists model with the FSE Item model.
     * A CustomerList has many FSE Items.
     * @param {object} models - The models to associate with.
     */
    CustomerLists.hasMany(models.FseItem, {
      foreignKey: "customer_id",
      as: "fseitems",
    });
  };

  return CustomerLists;
};
