'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class PurchaseOrder extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      PurchaseOrder.belongsTo(models.Store, { foreignKey: 'storeId', as: 'store' });
      PurchaseOrder.belongsTo(models.Supplier, { foreignKey: 'supplierId', as: 'supplier' });
      PurchaseOrder.belongsTo(models.User, { foreignKey: 'createdBy', as: 'creator' });
      PurchaseOrder.belongsTo(models.User, { foreignKey: 'approvedBy', as: 'approver' });
      PurchaseOrder.hasMany(models.PurchaseOrderItem, { foreignKey: 'purchaseOrderId', as: 'items' });  
    }
  }
  PurchaseOrder.init({
    supplierId: DataTypes.INTEGER,
    storeId: DataTypes.INTEGER,
    orderNumber: DataTypes.STRING,
    orderDate: DataTypes.DATE,
    expectedDeliveryDate: DataTypes.DATE,
    status: DataTypes.STRING,
    subtotal: DataTypes.DECIMAL,
    taxAmount: DataTypes.DECIMAL,
    shippingCost: DataTypes.DECIMAL,
    totalAmount: DataTypes.DECIMAL,
    notes: DataTypes.TEXT,
    createdBy: DataTypes.INTEGER,
    approvedBy: DataTypes.INTEGER,
    approvedAt: DataTypes.DATE
  }, {
    sequelize,
    modelName: 'PurchaseOrder',
  });
  return PurchaseOrder;
};