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

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

      receipt_no: {
        type: DataTypes.STRING(100),
        allowNull: false,
        unique: true,
      },

      date: { type: DataTypes.DATE, allowNull: true },
      customer: { type: DataTypes.STRING, allowNull: false },

      //   fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "companies",
          key: "id",
        },
      },
    },
    {
      tableName: "invoicereceipts",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Province model with other models.
   * @param {object} models - The models to associate with.
   */
  InvoiceReceipts.associate = (models) => {
    /**
     * Associates the InvoiceReceipts model with the InvoiceReceiptChild model.
     * A DeliveryTrip has many InvoiceReceiptChilds.
     * @param {object} models - The models to associate with.
     */
    InvoiceReceipts.hasMany(models.InvoiceReceiptChild, {
      foreignKey: "invoicereceipt_id",
      as: "invoicereceiptchilds",
      onDelete: 'CASCADE'
    });


    /**
     * Associates the InvoiceReceipts model with the Company model.
     * A DeliveryTrip belongs to a Company.
     * @param {object} models - The models to associate with.
     */
    InvoiceReceipts.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });

    /**
     * Associates the InvoiceReceipts model with the User model.
     * A DeliveryTrip belongs to a User.
     * @param {object} models - The models to associate with.
     */
    InvoiceReceipts.belongsTo(models.User, { foreignKey: 'owner', as: 'user' });

  };

  return InvoiceReceipts;
};
