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

module.exports = (sequelize) => {
  const TenderQuotation = sequelize.define(
    "tenderquotation",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      
      tendervendor_id: { type: DataTypes.INTEGER, allowNull: true },
      tender_id: { type: DataTypes.INTEGER, allowNull: true },

      item_id: { type: DataTypes.INTEGER, allowNull: true },
      unit: { type: DataTypes.STRING(50), allowNull: true },
      quantity: { type: DataTypes.INTEGER, allowNull: true },
      price: { type: DataTypes.DECIMAL(10, 2), allowNull: true },
      delivery_time: { type: DataTypes.INTEGER, allowNull: true },
      note: { type: DataTypes.TEXT, allowNull: true },
      submitted: { type: DataTypes.BOOLEAN, allowNull: true },
      submitted_at: { type: DataTypes.DATE, allowNull: true },
      is_winner: { type: DataTypes.BOOLEAN, allowNull: true },
      winner_at: { type: DataTypes.DATE, allowNull: true },
    },
    {
      tableName: "tenderquotations",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Province model with other models.
   * @param {object} models - The models to associate with.
   */
  TenderQuotation.associate = (models) => {
    TenderQuotation.belongsTo(models.TenderVendor, { foreignKey: "tendervendor_id" });
    TenderQuotation.belongsTo(models.Tender, { foreignKey: "tender_id", onDelete: "CASCADE" });
    TenderQuotation.belongsTo(models.TenderItem, { foreignKey: "item_id", as: "tenderitem" });
    TenderQuotation.hasMany(models.TenderQuotationFile, { foreignKey: "tenderquotation_id", as: "files", onDelete: "CASCADE" });
  };

  return TenderQuotation;
};
