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

module.exports = (sequelize) => {
  const Outlet = sequelize.define(
    "outlet",
    {
      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 },

      display_name: { type: DataTypes.STRING(100), allowNull: false },
      code: { type: DataTypes.STRING(255), allowNull: true },
      title: { type: DataTypes.STRING(100), allowNull: true },
      askes: { type: DataTypes.TINYINT, allowNull: true },
      jamkesmas: { type: DataTypes.TINYINT, allowNull: true },
      address: { type: DataTypes.TEXT, allowNull: true },
      phone: { type: DataTypes.STRING(100), allowNull: true },
      email: { type: DataTypes.STRING(255), allowNull: true },

      // fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "companies",
          key: "id",
        },
      },
      institution_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "institutions",
          key: "id",
        },
      },
      outlettype_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "outlettypes",
          key: "id",
        },
      },
      province_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "provinces",
          key: "id",
        },
      },
      city_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "cities",
          key: "id",
        },
      },

      // sappsv1 or erpnext
      ids: { type: DataTypes.INTEGER, allowNull: true },
      import_from: { type: DataTypes.STRING(100), allowNull: true },
    },
    {
      tableName: "outlets",
      freezeTableName: true,
    }
  );

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

    /**
     * Outlet belongs to User (modifier).
     */
    Outlet.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });

    /**
     * Associates the Outlet model with the Institution model.
     * @param {object} models - The models to associate with.
     */
    Outlet.belongsTo(models.Institution, { foreignKey: "institution_id", as: "institution" });

    /**
     * Associates the Outlet model with the OutletType model.
     * @param {object} models - The models to associate with.
     */
    Outlet.belongsTo(models.OutletType, { foreignKey: "outlettype_id", as: "outlettype" });

    /**
     * Associates the Outlet model with the Province model.
     * @param {object} models - The models to associate with.
     */
    Outlet.belongsTo(models.Province, { foreignKey: "province_id", as: "province" });

    /**
     * Associates the Outlet model with the City model.
     * @param {object} models - The models to associate with.
     */
    Outlet.belongsTo(models.City, { foreignKey: "city_id", as: "city" });

    /**
     * Associates the Outlet model with the TerritoryOutlet model.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.TerritoryOutlet, { foreignKey: "outlet_id", as: "territoryoutlets" });

    /**
     * Associates the Outlet model with the OutletClient model.
     * Defines a one-to-many relationship between Outlet and OutletClient.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.OutletClient, { foreignKey: "outlet_id", as: "outletclients" });

    /**
     * Associates the Outlet model with the Client model.
     * Defines a many-to-many relationship between Outlet and Client.
     * @param {object} models - The models to associate with.
     */
    Outlet.belongsToMany(models.Client, {
      through: models.OutletClient,
      foreignKey: "outlet_id",
      otherKey: "client_id",
      as: "clients",
    });
    /**
     * Associates the Outlet model with the CallPlanDestination model.
     * Defines a one-to-many relationship between Outlet and CallPlanDestination.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.CallPlanDestination, { foreignKey: "outlet_id", as: "callplandestinations" });

    /**
     * Associates the Outlet model with the CallPlanVisit model.
     * Defines a one-to-many relationship between Outlet and CallPlanVisit.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.CallPlanVisit, { foreignKey: "outlet_id", as: "callplanvisits" });

    /**
     * Associates the Outlet model with the CallPlanRealization model.
     * Defines a one-to-many relationship between Outlet and CallPlanRealization.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.CallPlanRealization, { foreignKey: "outlet_id", as: "callplanrealizations" });

    /**
     * Associates the Outlet model with the SalesDistributor model.
     * Defines a one-to-many relationship between Outlet and SalesDistributor.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.SalesDistributor, { foreignKey: "outlet_id", as: "salesdistributors" });

    /**
     * Associates the Outlet model with the MasterList model.
     * Defines a one-to-many relationship between Outlet and MasterList.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.MasterList, { foreignKey: "outlet_id", as: "masterlist_outlets" });

    /**
     * Associates the Outlet model with the ImportSalesDistributor model.
     * Defines a one-to-many relationship between Outlet and ImportSalesDistributor.
     * @param {object} models - The models to associate with.
     */
    Outlet.hasMany(models.ImportSalesDistributor, { foreignKey: "outlet_id", as: "importsalesdistributors" });
    Outlet.hasMany(models.ClientCollabDetail, { foreignKey: "outlet_id", as: "clientcollabdetails" });
    Outlet.hasMany(models.SurveySellingIn, { foreignKey: "outlet_id", as: "surveysellingins" });
    Outlet.hasMany(models.SurveyTreatment, { foreignKey: "outlet_id", as: "surveytreatments" });
    Outlet.hasMany(models.Udp, { foreignKey: "outlet_id", as: "udps" });
    Outlet.hasMany(models.UpCountryVisitList, { foreignKey: "outlet_id", as: "upcountryvisitlists" });
    Outlet.hasMany(models.DistributorOutlet, { foreignKey: "outlet_id", as: "distributoroutlets" });
  };

  return Outlet;
};
