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

module.exports = (sequelize) => {
  const SalesPlanEstimate = sequelize.define(
    "salesplanestimate",
    {
      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: true },
      owner: { type: DataTypes.INTEGER, allowNull: true },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      // fk
      salesplan_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "salesplans",
          key: "id",
        },
      },
      salesplanitem_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "salesplanitems",
          key: "id",
        },
      },

      week1_quantity: { type: DataTypes.INTEGER, allowNull: true },
      total_week1: { type: DataTypes.INTEGER, allowNull: true },
      week2_quantity: { type: DataTypes.INTEGER, allowNull: true },
      total_week2: { type: DataTypes.INTEGER, allowNull: true },
      week3_quantity: { type: DataTypes.INTEGER, allowNull: true },
      total_week3: { type: DataTypes.INTEGER, allowNull: true },
      week4_quantity: { type: DataTypes.INTEGER, allowNull: true },
      total_week4: { type: DataTypes.INTEGER, allowNull: true },
      week5_quantity: { type: DataTypes.INTEGER, allowNull: true },
      total_week5: { type: DataTypes.INTEGER, allowNull: true },      

      type: { type: DataTypes.STRING(50), allowNull: true },
      progress: {type: DataTypes.STRING(50), allowNull: true},
      remarks: { type: DataTypes.TEXT, allowNull: true },
   
    },
    {
      tableName: "salesplanestimates",
      freezeTableName: true,
    }
  );

  /**
   * Associates the SalesPlanEstimate model with other models.
   * @param {object} models - The models to associate with.
   * @description
   *   - A SalesPlanEstimate belongs to a SalesPlanItem.
   */
  SalesPlanEstimate.associate = function(models) {
    SalesPlanEstimate.belongsTo(models.SalesPlan, { foreignKey: "salesplan_id", as: "salesplan", onDelete: "CASCADE" });
    SalesPlanEstimate.belongsTo(models.SalesPlanItem, { foreignKey: "salesplanitem_id", as: "salesplanitem", onDelete: "CASCADE" });
  };

  return SalesPlanEstimate;
};
