const Sequelize = require("sequelize");
const sequelizeConnect = require("../connection/db");
const Products = require("./Products");
const SellersCompany = require("./SellersCompany");
const InventoryTransferDetails = require("./InventoryTransferDetails");

const InventoryTransfer = sequelizeConnect.define(
  "inventory_transfer",
  {
    id: {
      type: Sequelize.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
    },
    source_seller_id: {
      type: Sequelize.BIGINT,
      allowNull: false,
    },
    target_seller_id: {
      type: Sequelize.BIGINT,
      allowNull: false,
    },
    source_product_id: {
      type: Sequelize.BIGINT,
      allowNull: true,
    },
    target_product_id: {
      type: Sequelize.BIGINT,
      allowNull: true,
    },
    quantity: {
      type: Sequelize.DECIMAL(10, 2),
      allowNull: true,
    },
    approval_status: {
      type: Sequelize.ENUM("Approved", "Reject", "Pending"),
      allowNull: false,
      defaultValue: "Pending",
    },
    warehouse_transferred_out_initiator: {
      type: Sequelize.BIGINT,
      allowNull: true
    },
    warehouse_transferred_out_status: {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    },
    warehouse_transferred_out_attachment: {
      type: Sequelize.STRING,
      allowNull: true,
    },
    warehouse_transferred_in_initiator: {
      type: Sequelize.BIGINT,
      allowNull: true
    },
    warehouse_transferred_in_status: {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    },
    warehouse_transferred_in_attachment: {
      type: Sequelize.STRING,
      allowNull: true,
    },
    created_by: {
      type: Sequelize.STRING,
      allowNull: true,
    },
    approved_by: {
      type: Sequelize.STRING,
      allowNull: true,
    },
    createdAt: {
      type: Sequelize.DATE,
      allowNull: false,
    },
    updatedAt: {
      type: Sequelize.DATE,
      allowNull: false,
    },
  },
  {
    tableName: "inventory_transfer",
    timestamps: true,
  }
);

InventoryTransfer.belongsTo(Products, {
  foreignKey: "source_product_id",
  as: "source_product",
});
InventoryTransfer.belongsTo(Products, {
  foreignKey: "target_product_id",
  as: "target_product",
});
InventoryTransfer.belongsTo(SellersCompany, {
  foreignKey: "source_seller_id",
  as: "source_seller",
});
InventoryTransfer.belongsTo(SellersCompany, {
  foreignKey: "target_seller_id",
  as: "target_seller",
});

InventoryTransfer.hasMany(InventoryTransferDetails, {
  foreignKey: "inventory_transfer_id",
  as: "inventory_transfer_details",
});
InventoryTransferDetails.belongsTo(InventoryTransfer, {
  foreignKey: "inventory_transfer_id",
  as: "inventory_transfer",
});

module.exports = InventoryTransfer;
