const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const ProcurementTracker = sequelize.define(
    "procurmenttracker",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      code: { type: DataTypes.STRING(20), allowNull: true },
      vendor_name: { type: DataTypes.STRING(100), allowNull: true },
      from_nationality: { type: DataTypes.STRING(100), allowNull: true },
      estimated_arrival: { type: DataTypes.DATEONLY, allowNull: true },
      awb_no: { type: DataTypes.STRING(100), allowNull: true },
    },
    {
      tableName: "procurmenttrackers",
      freezeTableName: true,
    }
  );

  ProcurementTracker.associate = (models) => {
    ProcurementTracker.hasMany(models.ProcurementTrackerStatus, {
      foreignKey: "procurementtracker_id",
      as: "status_list",
    });
    ProcurementTracker.hasMany(models.ProcurementTrackerPO, {
      foreignKey: "procurementtracker_id",
      as: "po_list",
    });
    ProcurementTracker.hasMany(models.ProcurementTrackerAWB, {
      foreignKey: "procurementtracker_id",
      as: "awb_list",
    });
    ProcurementTracker.hasMany(models.ProcurementTrackerFile, {
      foreignKey: "procurementtracker_id",
      as: "files",
    });
    ProcurementTracker.hasMany(models.ProcurementTrackerInvoice, {
      foreignKey: "procurementtracker_id",
      as: "invoices",
    });
    ProcurementTracker.hasMany(models.ProcurementTrackerPackingList, {
      foreignKey: "procurementtracker_id",
      as: "packinglists",
    });
  };

  return ProcurementTracker;
};
