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

const PurchaseReceiveDetails = sequelizeConnect.define(
  "purchase_receive_details",
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
    },
    purchase_receive_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
    },
    purchase_order_detail_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      comment: "Links back to the original PO line item",
    },
    product_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
    },
    quantity_ordered: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: "Copied from PO for reference",
    },
    quantity_received: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
    unit_price: {
      type: DataTypes.DECIMAL(12, 2),
      allowNull: false,
      comment: "Copied from PO for reference",
    },
    expiry_date: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    condition: {
      type: DataTypes.ENUM("Good", "Damaged", "Expired"),
      allowNull: false,
      defaultValue: "Good",
    },
    status: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
  },
  {
    tableName: "purchase_receive_details",
    timestamps: true,
  }
);

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

module.exports = PurchaseReceiveDetails;
