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

const PurchaseReceives = sequelizeConnect.define(
  "purchase_receives",
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
    },
    receive_reference_no: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
    },
    purchase_order_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
    },
    receive_date: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    delivery_reference_no: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: "Delivery note number from supplier",
    },
    delivery_date: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    warehouse_id: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: "Where goods are being received",
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    status: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
    verified_received: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    },
    created_by_admin_token: {
      type: DataTypes.STRING(500),
      allowNull: true,
    },
    updated_by_admin_token: {
      type: DataTypes.STRING(500),
      allowNull: true,
    },
  },
  {
    tableName: "purchase_receives",
    timestamps: true,
  }
);

PurchaseReceives.hasMany(PurchaseReceiveDetails, {
  foreignKey: "purchase_receive_id",
  as: "purchase_receive_details",
});
PurchaseReceiveDetails.belongsTo(PurchaseReceives, {
  foreignKey: "purchase_receive_id",
  as: "purchase_receive",
});

module.exports = PurchaseReceives;
