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

module.exports = (sequelize) => {
  const SalodAwalClient = sequelize.define(
    "saldoawalclient",
    {
      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
      client_id: { type: DataTypes.INTEGER, allowNull: false },
      item_id: { type: DataTypes.INTEGER, allowNull: false },
      balance: { type: DataTypes.DECIMAL(20, 2), allowNull: false },
      type: { type: DataTypes.STRING(50), allowNull: false },
      is_imported: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: false },
      error_msg: { type: DataTypes.TEXT, allowNull: true },
      filename: { type: DataTypes.STRING(255), allowNull: true },
    },
    {
      tableName: "saldoawalclients",
      freezeTableName: true,
    }
  );

  /**
   * Associates the SalodAwalClient model with other models.
   * @param {object} models - The models to associate with.
   */
  SalodAwalClient.associate = (models) => {
    SalodAwalClient.belongsTo(models.Client, { foreignKey: "client_id", as: "client" });
    SalodAwalClient.belongsTo(models.Item, { foreignKey: "item_id", as: "item" });
  };

  return SalodAwalClient;
};
