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

module.exports = (sequelize) => {
  const TerritoryOutlet = sequelize.define(
    "territoryoutlet",
    {
      territory_id: {
        type: DataTypes.INTEGER,
        references: {
          model: "territories",
          key: "id",
          primaryKey: true,
        },
      },
      outlet_id: {
        type: DataTypes.INTEGER,
        references: {
          model: "outlets",
          key: "id",
          primaryKey: true,
        },
      },
      valid_from: {
        type: DataTypes.DATEONLY,
        allowNull: true,
      },
      valid_to: {
        type: DataTypes.DATEONLY,
        allowNull: true,
      },
    },
    {
      tableName: "territoryoutlets",
      freezeTableName: true,
    }
  );

  /**
   * Associates TerritoryOutlet with other models.
   * @param {object} models - The models to associate with.
   * @description
   *   - A TerritoryOutlet belongs to a Territory.
   *   - A TerritoryOutlet belongs to an Outlet.
   *   - The association is labeled as 'Outlet'.
   */
  TerritoryOutlet.associate = (models) => {
    /**
     * Associate TerritoryOutlet with Territory
     * @param {object} models - The models to associate with.
     * @description
     *   - A TerritoryOutlet belongs to a Territory.
     */
    TerritoryOutlet.belongsTo(models.Territory, {
      foreignKey: "territory_id",
      as: "territory",
    });

    /**
     * Associate TerritoryOutlet with Outlet
     * @param {object} models - The models to associate with.
     * @description
     *   - A TerritoryOutlet belongs to an Outlet.
     *   - The association is labeled as 'Outlet'.
     */
    TerritoryOutlet.belongsTo(models.Outlet, {
      foreignKey: "outlet_id",
      as: "outlet",
    });
  };

  return TerritoryOutlet;
};
