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

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

      address_name: { type: DataTypes.STRING, allowNull: true },
      latitude: { type: DataTypes.STRING, allowNull: true },
      longitude: { type: DataTypes.STRING, allowNull: true },

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


  UpCountryDistance.associate = (models) => {
    UpCountryDistance.belongsTo(models.UpCountry, { foreignKey: "upcountry_id", as: "upcountry", onDelete: "CASCADE" });
  };

  return UpCountryDistance;
};
