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

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

      stock_uom: { type: DataTypes.STRING(100), allowNull: true },
      item_price: { type: DataTypes.DECIMAL(29, 2), allowNull: true },
      disc_distributor: { type: DataTypes.DECIMAL(5, 2), allowNull: true },
      disc_principal: { type: DataTypes.DECIMAL(5, 2), allowNull: true },
      quantity: { type: DataTypes.INTEGER, allowNull: true },
      total: { type: DataTypes.DECIMAL(29, 2), allowNull: true },

      // fk
      udp_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "udps",
          key: "id",
        },
      },
      item_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "items",
          key: "id",
        },
      },
    },
    {
      tableName: "udpproducts",
      freezeTableName: true,
    }
  );

  UdpProduct.associate = (models) => {
    UdpProduct.belongsTo(models.Udp, { foreignKey: "udp_id", onDelete: "CASCADE" });
    UdpProduct.belongsTo(models.Item, { foreignKey: "item_id", as: "item" });
  };

  return UdpProduct;
};
