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

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

      name: { type: DataTypes.STRING(100), allowNull: true },
      item_code: { type: DataTypes.STRING(100), allowNull: true },
      sku_code: { type: DataTypes.STRING(100), allowNull: true },
      batch: { type: DataTypes.STRING(100), allowNull: true },
      principal: { type: DataTypes.STRING(100), allowNull: true },
      ed_stock: { type: DataTypes.DATEONLY, allowNull: true },
      stock_qty: { type: DataTypes.INTEGER, allowNull: true },
      arrival_date: { type: DataTypes.DATEONLY, allowNull: true },
      price: { type: DataTypes.INTEGER, allowNull: true },
      order_qty: { type: DataTypes.INTEGER, allowNull: true },
      discount: { type: DataTypes.INTEGER, allowNull: true },
      disc_elr: { type: DataTypes.INTEGER, allowNull: true },
      discount_price: { type: DataTypes.INTEGER, allowNull: true },
      total_product: { type: DataTypes.INTEGER, allowNull: true },

      //fk
      udk_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "udks",
          key: "id",
        },
      },

    },
    {
      tableName: "udkproducts",
      freezeTableName: true,
    }
  );

  /**
   * Associates the UdkProduct model with other models.
   * @param {object} models - The models to associate with.
   * @description
   *   - A UdkProduct belongs to a Udk.
   */
  UdkProduct.associate = (models) => {
    /**
     * Associates the UdkProduct model with the Udk model.
     * A UdkProduct belongs to a Udk.
     * @param {object} models - The models to associate with.
     */
   UdkProduct.belongsTo(models.Udk, {
      foreignKey: "udk_id",
      as: "udk",
    });
  };

  return UdkProduct;
};
