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

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

      outlet_id: { type: DataTypes.INTEGER, allowNull: true },
      outlet_name: { type: DataTypes.STRING, allowNull: true },
      client_id: { type: DataTypes.INTEGER, allowNull: true },
      client_name: { type: DataTypes.STRING, allowNull: true },
      items: { type: DataTypes.JSON, allowNull: true },

      // fk
      upcountry_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "upcountries",
          key: "id",
        },
      },
    },
    {
      tableName: "upcountryvisitlists",
      freezeTableName: true,
    }
  );

  UpCountryVisitList.associate = (models) => {
    UpCountryVisitList.belongsTo(models.UpCountry, {
      foreignKey: "upcountry_id",
      as: "upcountry",
      onDelete: "CASCADE",
    });
    UpCountryVisitList.belongsTo(models.Outlet, { foreignKey: "outlet_id", as: "outlet" });
    UpCountryVisitList.belongsTo(models.Client, { foreignKey: "client_id", as: "client" });
  };

  return UpCountryVisitList;
};
