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

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

      type: { type: DataTypes.STRING, allowNull: true },
      gimmickreq_id: { type: DataTypes.INTEGER, allowNull: true },
      surveyexpense_id: { type: DataTypes.INTEGER, allowNull: true },

      item_id: { type: DataTypes.INTEGER, allowNull: true },
      sales_id: { type: DataTypes.INTEGER, allowNull: true },
      outlet_id: { type: DataTypes.INTEGER, allowNull: true },
      client_id: { type: DataTypes.INTEGER, allowNull: false, reference: { model: "clients", key: "id" } },
      territory_id: { type: DataTypes.INTEGER, allowNull: true },
      period_id: { type: DataTypes.INTEGER, allowNull: true },
      year_id: { type: DataTypes.INTEGER, allowNull: true },
      kerja_sama: { type: DataTypes.INTEGER, allowNull: true },
      quantity: { type: DataTypes.INTEGER, allowNull: true },
      status: { type: DataTypes.INTEGER, allowNull: true },
    },
    {
      tableName: "accountentries",
      freezeTableName: true,
    }
  );

  AccountEntries.associate = (models) => {
    AccountEntries.belongsTo(models.SurveyExpense, {
      foreignKey: "surveyexpense_id",
      as: "surveyexpense",
      onDelete: "CASCADE",
    });
    AccountEntries.belongsTo(models.GimmickRequest, {
      foreignKey: "gimmickreq_id",
      as: "gimmickreq",
      onDelete: "CASCADE",
    });
    AccountEntries.belongsTo(models.Item, { foreignKey: "item_id", as: "item", onDelete: "CASCADE" });
    AccountEntries.belongsTo(models.SalesDistributor, { foreignKey: "sales_id", as: "sales", onDelete: "CASCADE" });
    AccountEntries.belongsTo(models.Outlet, { foreignKey: "outlet_id", as: "outlet", onDelete: "CASCADE" });
    AccountEntries.belongsTo(models.Client, { foreignKey: "client_id", as: "client", onDelete: "CASCADE" });
    AccountEntries.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory", onDelete: "CASCADE" });
    AccountEntries.belongsTo(models.Period, { foreignKey: "period_id", as: "period", onDelete: "CASCADE" });
    AccountEntries.belongsTo(models.Year, { foreignKey: "year_id", as: "year", onDelete: "CASCADE" });
  };

  return AccountEntries;
};
