const { DataTypes } = require("sequelize");
const sequelizeConnect = require("../connection/db");
const Products = require("./Products");

const PurchaseOrderDetails = sequelizeConnect.define(
  "purchase_order_details",
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
    },
    purchase_order_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
    },
    product_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
    },
    quantity: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
    unit_price: {
      type: DataTypes.DECIMAL(12, 2),
      allowNull: false,
    },
    tax_code: {
      type: DataTypes.BIGINT,
      allowNull: true,
    },
    tax_rate: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
    },
    total_amount: {
      type: DataTypes.DECIMAL(14, 2),
      allowNull: false,
    },
    status: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
  },
  {
    tableName: "purchase_order_details",
    timestamps: true,
  }
);

PurchaseOrderDetails.belongsTo(Products, {
  foreignKey: "product_id",
  as: "product"
})

module.exports = PurchaseOrderDetails;
