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

module.exports = (sequelize) => {
  const Tender = sequelize.define(
    "tender",
    {
      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(100), allowNull: true },
      description: { type: DataTypes.TEXT, allowNull: true },
      metode: { type: DataTypes.STRING(50), allowNull: true },
      status: { type: DataTypes.STRING(100), allowNull: true },
      start_date: { type: DataTypes.DATE, allowNull: true },
      end_date: { type: DataTypes.DATE, allowNull: true },

      link: { type: DataTypes.TEXT, allowNull: true },

      tendercategory_id: { type: DataTypes.INTEGER, allowNull: true },
      tenderpaymentterm_id: { type: DataTypes.INTEGER, allowNull: true },
    },
    {
      tableName: "tenders",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Province model with other models.
   * @param {object} models - The models to associate with.
   */
  Tender.associate = (models) => {
    Tender.hasMany(models.TenderFile, { foreignKey: "tender_id", as: "files", onDelete: "CASCADE"});
    Tender.hasMany(models.TenderItem, { foreignKey: "tender_id", as: "items", onDelete: "CASCADE"});
    Tender.belongsTo(models.TenderCategory, { foreignKey: "tendercategory_id", as: "tendercategory" });
    Tender.belongsTo(models.TenderPaymentTerm, { foreignKey: "tenderpaymentterm_id", as: "tenderpaymentterm" });
    Tender.hasMany(models.TenderInvitation, { foreignKey: "tender_id", as: "invitations", onDelete: "CASCADE"});
    Tender.hasMany(models.TenderQuotation, { foreignKey: "tender_id", as: "quotations", onDelete: "CASCADE"});
  };

  return Tender;
};
