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

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

      customer: { type: DataTypes.STRING(100), allowNull: false },
      delivery_note:  { type: DataTypes.JSON, allowNull: true },
      address: { type: DataTypes.STRING(250), allowNull: true },
      receiver: { type: DataTypes.STRING(100), allowNull: true },
      start: { type: DataTypes.STRING(50), allowNull: true },
      end: { type: DataTypes.STRING(50), allowNull: true },
      total_time: { type: DataTypes.STRING(50), allowNull: true },
      send_date: { type: DataTypes.DATEONLY, allowNull: true },
      receive_date: { type: DataTypes.DATEONLY, allowNull: true },
      sign: { type: DataTypes.TEXT, allowNull: true },

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

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

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

  return DeliveryTripChild;
};
