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

module.exports = (sequelize) => {
  const Distributor = sequelize.define(
    "distributor",
    {
      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: true },
      owner: { type: DataTypes.INTEGER, allowNull: true },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      display_name: { type: DataTypes.STRING(100), allowNull: false },
      code: { type: DataTypes.STRING(100), allowNull: false },

      // fk
      company_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "companies", key: "id" } },
    },
    {
      tableName: "distributors",
      freezeTableName: true,
    }
  );

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

    /**
     * Associates the Distributor model with the DistributorOutlet model.
     * A Distributor has many DistributorOutlets.
     * @param {object} models - The models to associate with.
     */
    Distributor.hasMany(models.DistributorOutlet, { foreignKey: "distributor_id", as: "outlets" });

    /**
     * Associates the Distributor model with the DistributorProduct model.
     * A Distributor has many DistributorProducts.
     * @param {object} models - The models to associate with.
     */
    Distributor.hasMany(models.DistributorProduct, { foreignKey: "distributor_id", as: "products" });

    /**
     * Associates the Distributor model with the DistributorBranch model.
     * A Distributor has many DistributorBranches.
     * @param {object} models - The models to associate with.
     */
    Distributor.hasMany(models.DistributorBranch, { foreignKey: "distributor_id", as: "branches" });

    /**
     * Associates the Distributor model with the SalesDistributor model.
     * A Distributor has many SalesDistributors.
     * @param {object} models - The models to associate with.
     */
    Distributor.hasMany(models.SalesDistributor, { foreignKey: "distributor_id", as: "salesdistributors" });

    /**
     * Associates the Distributor model with the ImportSalesDistributor model.
     * A Distributor has many ImportSalesDistributors.
     * @param {object} models - The models to associate with.
     */
    Distributor.hasMany(models.ImportSalesDistributor, { foreignKey: "distributor_id", as: "importsalesdistributors" });
    Distributor.hasMany(models.Udp, { foreignKey: "distributor_id", as: "udps" });
  };

  return Distributor;
};
