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

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


      item_type: { type: DataTypes.STRING(100), allowNull: false },
      description: { type: DataTypes.STRING(250), allowNull: true },
      price: { type: DataTypes.INTEGER, allowNull: true },

      //fk
      deliverytrip_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "deliverytrips",
          key: "id",
        },
      },

    },
    {
      tableName: "deliverytripcosts",
      freezeTableName: true,
    }
  );

  /**
   * Associates the DeliveryTripCost model with other models.
   * @param {object} models - The models to associate with.
   * @description
   *   - A DeliveryTripCost belongs to a DeliveryTrip.
   */
  DeliveryTripCost.associate = (models) => {
    /**
     * Associates the DeliveryTripCost model with the DeliveryTrip model.
     * A DeliveryTripCost belongs to a DeliveryTrip.
     * @param {object} models - The models to associate with.
     */
   DeliveryTripCost.belongsTo(models.DeliveryTrip, {
      foreignKey: "deliverytrip_id",
      as: "deliverytrip",
    });
  };

  return DeliveryTripCost;
};
