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

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

      no_invoice: { type: DataTypes.STRING(100), allowNull: false },
      transaction_date: { type: DataTypes.DATEONLY, allowNull: false },
      discount: { type: DataTypes.DECIMAL(10, 2), allowNull: true },
      price: { type: DataTypes.INTEGER, allowNull: true },
      quantity: { type: DataTypes.INTEGER, allowNull: true },
      total: { type: DataTypes.INTEGER, allowNull: true },

      // fk
      company_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "companies", key: "id" } },
      outlet_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "outlets", key: "id" } },
      distributor_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "distributors", key: "id" } },
      item_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "items", key: "id" } },
      period_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "periods", key: "id" } },
      territory_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "territories", key: "id" } },
    },
    {
      tableName: "salesdistributors",
      freezeTableName: true,
    }
  );

  SalesDistributor.associate = (models) => {
    SalesDistributor.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
    SalesDistributor.belongsTo(models.Outlet, { foreignKey: "outlet_id", as: "outlet" });
    SalesDistributor.belongsTo(models.Distributor, { foreignKey: "distributor_id", as: "distributor" });
    SalesDistributor.belongsTo(models.Period, { foreignKey: "period_id", as: "period" });
    SalesDistributor.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory" });
    SalesDistributor.belongsTo(models.Item, { foreignKey: "item_id", as: "item" });
  };

  return SalesDistributor;
};
