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

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

      // main
      request_date: { type: DataTypes.DATE, allowNull: true },
      doc_no: { type: DataTypes.STRING, allowNull: true },
      tax_type: { type: DataTypes.STRING, allowNull: true },
      tax_percent: { type: DataTypes.INTEGER, allowNull: true },
      periode: { type: DataTypes.INTEGER, allowNull: true },
      total: { type: DataTypes.DECIMAL, allowNull: true },
      dpp: { type: DataTypes.DECIMAL, allowNull: true },
      tax_value: { type: DataTypes.DECIMAL, allowNull: true },
      note: { type: DataTypes.TEXT, allowNull: true },
      nik: { type: DataTypes.STRING, allowNull: true },
      remarks_realization: { type: DataTypes.TEXT, allowNull: true },
      transfer_scheduled_date: { type: DataTypes.DATE, allowNull: true },
      transfer_realization_date: { type: DataTypes.DATE, allowNull: true },
      transfer_status: { type: DataTypes.STRING, allowNull: true },

      // fk
      gimmickreqtype_id: { type: DataTypes.INTEGER, allowNull: true },
      gimmickrequestsubtype_id: { type: DataTypes.INTEGER, allowNull: true },
      client_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: "clients", key: "id" },
      },
      territory_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: "territories", key: "id" },
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      employee_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },

      is_balance_posted: { type: DataTypes.BOOLEAN, allowNull: true },
    },
    {
      tableName: "gimmickrequests",
      freezeTableName: true,
    }
  );

  /**
   * Associates the GimmickRequest model with other models.
   * @param {object} models - The models to associate with.
   * @description
   */
  GimmickRequest.associate = (models) => {
    GimmickRequest.hasMany(models.AccountEntries, {
      foreignKey: "gimmickreq_id",
      as: "accountentries",
      onDelete: "CASCADE",
    });
    GimmickRequest.belongsTo(models.GimmickReqType, {
      foreignKey: "gimmickreqtype_id",
      as: "gimmickrequesttype",
    });
    GimmickRequest.belongsTo(models.GimmickReqSubType, {
      foreignKey: "gimmickrequestsubtype_id",
      as: "gimmickrequestsubtype",
    });
    GimmickRequest.belongsTo(models.Client, {
      foreignKey: "client_id",
      as: "client",
    });
    GimmickRequest.belongsTo(models.Territory, {
      foreignKey: "territory_id",
      as: "territory",
    });
    GimmickRequest.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });
    GimmickRequest.hasMany(models.GimmickAllocation, {
      foreignKey: "gimmickreq_id",
      as: "gimmickallocations",
    });
    GimmickRequest.hasMany(models.GimmickReceiver, {
      foreignKey: "gimmickreq_id",
      as: "gimmickreceivers",
    });
    GimmickRequest.belongsTo(models.Employee, {
      foreignKey: "employee_id",
      as: "employee",
    });
  };

  return GimmickRequest;
};
