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

module.exports = (sequelize) => {
  const DistributorOutlet = sequelize.define(
    "distributoroutlet",
    {
      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},
      city: { type: DataTypes.STRING(100), allowNull: true},
      address: { type: DataTypes.TEXT, allowNull: true},
      sap_address: { type: DataTypes.TEXT, allowNull: true},
      sap_city: { type: DataTypes.STRING(100), allowNull: true},

      // fk
      outlet_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "outlets", key: "id" } },
      distributor_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "distributors", key: "id" } },
    },
    {
      tableName: "distributoroutlets",
      freezeTableName: true,
    }
  );

  /**
   * Associates the DistributorOutlet model with other models.
   * @param {object} models - The models to associate with.
   */
  DistributorOutlet.associate = (models) => {
    /**
     * Associates the DistributorOutlet model with the Outlet model.
     * A DistributorOutlet belongs to an Outlet.
     * @param {object} models - The models to associate with.
     */
    DistributorOutlet.belongsTo(models.Outlet, { foreignKey: "outlet_id", as: "outlet", onDelete: "CASCADE" });

    /**
     * Associates the DistributorOutlet model with the Distributor model.
     * A DistributorOutlet belongs to a Distributor.
     * @param {object} models - The models to associate with.
     */
    DistributorOutlet.belongsTo(models.Distributor, { foreignKey: "distributor_id", as: "distributor", onDelete: "CASCADE" });
  };

  return DistributorOutlet;
};
