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

module.exports = (sequelize) => {
  const Item = sequelize.define(
    "Item",
    {
      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 },
      display_name: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      therapy_class: { type: DataTypes.STRING, allowNull: true },
      code: { type: DataTypes.STRING(30), allowNull: true },
      price: { type: DataTypes.DECIMAL(29, 2), allowNull: true },
      item_cor: { type: DataTypes.FLOAT, allowNull: true },
      stock_uom: { type: DataTypes.STRING, allowNull: true },
      sku_code: { type: DataTypes.STRING, allowNull: true },
      alternative_sku_code: { type: DataTypes.STRING, allowNull: true },
      image: { type: DataTypes.TEXT, allowNull: true },
      standard_rate: { type: DataTypes.STRING, allowNull: true },
      brand: { type: DataTypes.STRING, allowNull: true },
      conversion_factor: { type: DataTypes.STRING, allowNull: true },
      valid_from: { type: DataTypes.DATE, allowNull: true },
      valid_to: { type: DataTypes.DATE, allowNull: true },
      import_from: { type: DataTypes.STRING(100), allowNull: true },
      idx: { type: DataTypes.STRING, allowNull: true },
      ids: { type: DataTypes.INTEGER, allowNull: true },
      itemgroup_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: "itemgroups", key: "id" },
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: "companies", key: "id" },
      },
    },
    {
      tableName: "items",
    },
    {
      freezeTableName: true,
    }
  );

  /**
   * Defines associations between Item and other models.
   * @param {object} models - The models to associate with.
   */
  Item.associate = (models) => {
    /**
     * An Item belongs to an ItemGroup.
     * Defines a many-to-one relationship between Item and ItemGroup.
     */
    Item.belongsTo(models.ItemGroup, {
      foreignKey: "itemgroup_id",
      as: "itemgroups", // Alias for the association
    });

    /**
     * An Item has many ItemCompetitors.
     * Defines a one-to-many relationship between Item and ItemCompetitor.
     */
    Item.hasMany(models.ItemCompetitor, {
      foreignKey: "item_id",
      as: "itemcompetitors", // Alias for the association
    });

    /**
     * An Item belongs to a Company.
     * Defines a many-to-one relationship between Item and Company.
     */
    Item.belongsTo(models.Company, {
      foreignKey: "company_id", // Foreign key in the Item model referencing Company
      as: "company", // Alias for the association
    });

    /**
     * An Item has many SalesDistributors.
     * Defines a one-to-many relationship between Item and SalesDistributor.
     */
    Item.hasMany(models.SalesDistributor, {
      foreignKey: "item_id", // Foreign key in the SalesDistributor model referencing Item
      as: "salesdistributors", // Alias for the association
    });

    Item.hasMany(models.ImportSalesDistributor, {
      foreignKey: "item_id", // Foreign key in the SalesDistributor model referencing Item
      as: "importsalesdistributors", // Alias for the association
    });

    Item.hasMany(models.ClientCollabDetail, {
      foreignKey: "item_id", // Foreign key in the SalesDistributor model referencing Item
      as: "clientcollabsdetails", // Alias for the association
    });

    Item.hasMany(models.UdpProduct, {
      foreignKey: "item_id", // Foreign key in the SalesDistributor model referencing Item
      as: "udpproducts", // Alias for the association
    });

    Item.hasMany(models.SalesTargetDetail, {
      foreignKey: "item_id", // Foreign key in the SalesTarget model referencing Item
      as: "salestargetdetails", // Alias for the association
    });

    Item.hasMany(models.SalesPlanItem, {
      foreignKey: "item_id", // Foreign key in the SalesPlan model referencing Item
      as: "salesplanitems", // Alias for the association
    });

    Item.hasMany(models.FseItem, {
      foreignKey: "item_id", // Foreign key in the SalesDistributor model referencing Item
      as: "fseitems", // Alias for the association
    });

    Item.hasMany(models.AccountEntries, {
      foreignKey: "item_id", // Foreign key in the AccountEntries model referencing Item
      as: "accountentries", // Alias for the association
    });

    Item.hasMany(models.DistributorProduct, {
      foreignKey: "item_id", // Foreign key in the DistributorProduct model referencing Item
      as: "distributorproducts", // Alias for the association
    });
  };

  return Item;
};
