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

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

      product_name: { type: DataTypes.STRING, allowNull: false },
      item_code: { type: DataTypes.STRING, allowNull: false },
      quantity: { type: DataTypes.INTEGER, allowNull: true },
      batch_no: { type: DataTypes.STRING, allowNull: true },
      expiry_date: { type: DataTypes.DATE, allowNull: true },
      return_reason: { type: DataTypes.STRING, allowNull: true },
      distributor_note: { type: DataTypes.TEXT, allowNull: true },
      company_note: { type: DataTypes.TEXT, allowNull: true },
      product_note: { type: DataTypes.TEXT, allowNull: true },
      status: { type: DataTypes.STRING, allowNull: true },

      //fk
      productreturn_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "productreturns",
          key: "id",
        },
      },
    },
    {
      tableName: "productreturnchilds",
      freezeTableName: true,
    }
  );

  /**
   * @param {object} models - The models to associate with.
   * @description
   *   - A ProductReturnChild belongs to a ProductReturn.
   */
  ProductReturnChild.associate = (models) => {
    /**
     * Associates the ProductReturnChild model with the ProductReturn model.
     * @param {object} models - The models to associate with.
     */

    ProductReturnChild.belongsTo(models.ProductReturn, {
      foreignKey: "productreturn_id",
      as: "productreturn",
    });
  };

  return ProductReturnChild;
};
