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

module.exports = (sequelize) => {
  const ProductReturns = sequelize.define(
    "productreturn",
    {
      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 },

      barp_company: { type: DataTypes.STRING, allowNull: true },
      barp_distributor: { type: DataTypes.STRING, allowNull: true },
      barp_date: { type: DataTypes.DATE, allowNull: true },
      distributor_name: { type: DataTypes.STRING, allowNull: true },
      branch: { type: DataTypes.STRING, allowNull: true },
      
      
      nota_no: { type: DataTypes.STRING, allowNull: true },
      nota_date: { type: DataTypes.DATE, allowNull: true },
      nota_senddate: { type: DataTypes.DATE, allowNull: true },
      nota_receiptdate: { type: DataTypes.DATE, allowNull: true },
      return_file: { type: DataTypes.TEXT, allowNull: true },

      dr_no: { type: DataTypes.STRING, allowNull: false },
      dr_date: { type: DataTypes.DATE, allowNull: true },
      dr_file: { type: DataTypes.TEXT, allowNull: true },

      //   fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "companies",
          key: "id",
        },
      },
    },
    {
      tableName: "productreturns",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Applicant model with other models.
   * @param {object} models - The models to associate with.
   */
  ProductReturns.associate = (models) => {
    ProductReturns.hasMany(models.ProductReturnChild, {
      foreignKey: "productreturn_id",
      as: "productreturnchilds",
      onDelete: 'CASCADE'
    });

    ProductReturns.hasMany(models.ProductReturnFU, {
      foreignKey: "productreturn_id",
      as: "productreturnfus",
      onDelete: 'CASCADE'
    });

    /**
     * Associates the Applicants model with the Company model.
     * A Model belongs to a Company.
     * @param {object} models - The models to associate with.
     */
    ProductReturns.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });

    ProductReturns.belongsTo(models.User, { foreignKey: "owner", as: "user" });
  };

  return ProductReturns;
};
