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

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

      vendor_name: { type: DataTypes.STRING(100), allowNull: false },
      email: { type: DataTypes.STRING(100), allowNull: false },
      phone: { type: DataTypes.STRING(20), allowNull: true },
      address: { type: DataTypes.STRING(255), allowNull: true },
      pic_name: { type: DataTypes.STRING(100), allowNull: true },
      pic_phone: { type: DataTypes.STRING(20), allowNull: true },
      pic_email: { type: DataTypes.STRING(100), allowNull: true },

      category: { type: DataTypes.JSON, allowNull: true },

      status: { type: DataTypes.STRING(20), allowNull: true },
      user_id: { type: DataTypes.INTEGER, allowNull: true },
    },
    {
      tableName: "tendervendors",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Province model with other models.
   * @param {object} models - The models to associate with.
   */
  TenderVendor.associate = (models) => {
    TenderVendor.hasMany(models.TenderInvitation, { foreignKey: "tendervendor_id", as: "invitations", onDelete: "CASCADE"});
    TenderVendor.hasMany(models.TenderQuotation, { foreignKey: "tendervendor_id", as: "quotations", onDelete: "CASCADE"});
    TenderVendor.hasOne(models.TenderVendorFile, { foreignKey: "tendervendor_id", as: "tendervendorfile", onDelete: "CASCADE"});
    TenderVendor.belongsTo(models.User, { foreignKey: "user_id", as: "user"});
  };

  return TenderVendor;
};
