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

module.exports = (sequelize) => {
  const NCProductChild = sequelize.define(
    "ncproductchild",
    {
      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: true },
      quantity: { type: DataTypes.INTEGER, allowNull: true },
      uom: { type: DataTypes.STRING, allowNull: true },
      batch_no: { type: DataTypes.STRING, allowNull: true },
      expiry_date: { type: DataTypes.DATE, allowNull: true },
      noncomformity: { type: DataTypes.STRING, allowNull: true },
      description: { type: DataTypes.TEXT, allowNull: true },
      status: { type: DataTypes.STRING, allowNull: true },
      status_date: { type: DataTypes.DATE, allowNull: true },

      //fk
      ncproduct_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "ncproducts",
          key: "id",
        },
      },
    },
    {
      tableName: "ncproductchilds",
      freezeTableName: true,
    }
  );

  /**
   * @param {object} models - The models to associate with.
   * @description
   *   - A DeliveryTripChild belongs to a DeliveryTrip.
   */
  NCProductChild.associate = (models) => {
    /**
     * Associates the DeliveryTripChild model with the DeliveryTrip model.
     * A DeliveryTripChild belongs to a DeliveryTrip.
     * @param {object} models - The models to associate with.
     */
    NCProductChild.belongsTo(models.NcProduct, {
      foreignKey: "ncproduct_id",
      as: "ncproduct",
    });
  };

  return NCProductChild;
};
