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

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

      delivery_type: {
        type: DataTypes.STRING(20),
        allowNull: false,
        defaultValue: "internal",
      },
      expedition_vendor_id: { type: DataTypes.INTEGER, allowNull: true },
      resi_number: { type: DataTypes.STRING(100), allowNull: true },
      pick_up_time: { type: DataTypes.STRING(50), allowNull: true },
      cartons_pcs: { type: DataTypes.INTEGER, allowNull: true },
      weight_kg: { type: DataTypes.DECIMAL(10, 2), allowNull: true },
      dimensions_cm: { type: DataTypes.STRING(100), allowNull: true },
      volume_calculated: { type: DataTypes.DECIMAL(12, 4), allowNull: true },

      delivery_no: {
        type: DataTypes.STRING(100),
        allowNull: false,
        unique: true,
      },

      date: { type: DataTypes.DATE, allowNull: false },
      departure_time: { type: DataTypes.STRING(100), allowNull: true },
      driver: { type: DataTypes.INTEGER, allowNull: true },
      status: { type: DataTypes.INTEGER, allowNull: false },

      in_odometer: { type: DataTypes.INTEGER, allowNull: true },
      out_odometer: { type: DataTypes.INTEGER, allowNull: true },
      current_odometer: { type: DataTypes.INTEGER, allowNull: true },
      used_odometer: { type: DataTypes.INTEGER, allowNull: true },
      in_time: { type: DataTypes.STRING(50), allowNull: true },
      out_time: { type: DataTypes.STRING(50), allowNull: true },
      delivery_area: { type: DataTypes.STRING(100), allowNull: true },
      
      total_cost: { type: DataTypes.INTEGER, allowNull: true },
      date: { type: DataTypes.DATE, allowNull: false },
      notes: { type: DataTypes.TEXT, allowNull: true },
      // delivery_stop: { type: DataTypes.JSON, allowNull: true },

      //   fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "companies",
          key: "id",
        },
      },
      vehicle_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "vehicles",
          key: "id",
        },
      },
    },
    {
      tableName: "deliverytrips",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Province model with other models.
   * @param {object} models - The models to associate with.
   */
  DeliveryTrips.associate = (models) => {
    /**
     * Associates the DeliveryTrips model with the DeliveryTripChild model.
     * A DeliveryTrip has many DeliveryTripChilds.
     * @param {object} models - The models to associate with.
     */
    DeliveryTrips.hasMany(models.DeliveryTripChild, {
      foreignKey: "deliverytrip_id",
      as: "deliverytripchilds",
      onDelete: 'CASCADE'
    });
    /**
     * Associates the DeliveryTrips model with the DeliveryTripCost model.
     * A DeliveryTrip has many DeliveryTripCosts.
     * @param {object} models - The models to associate with.
     */
    DeliveryTrips.hasMany(models.DeliveryTripCost, {
      foreignKey: "deliverytrip_id",
      as: "deliverytripcosts",
      onDelete: 'CASCADE'
    });

    /**
     * Associates the DeliveryTrips model with the Company model.
     * A DeliveryTrip belongs to a Company.
     * @param {object} models - The models to associate with.
     */
    DeliveryTrips.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });

    /**
     * Associates the DeliveryTrips model with the Vehicle model.
     * A DeliveryTrip belongs to a Vehicle.
     * @param {object} models - The models to associate with.
     */
    DeliveryTrips.belongsTo(models.Vehicle, {
      foreignKey: "vehicle_id",
      as: "vehicle",
    });

    /**
     * Associates the DeliveryTrips model with the Company model.
     * A DeliveryTrip belongs to a Company.
     * @param {object} models - The models to associate with.
     */
    DeliveryTrips.belongsTo(models.Employee, {
      foreignKey: "driver",
      as: "driver_info",
    });

    DeliveryTrips.belongsTo(models.User, { foreignKey: 'owner', as: 'user' });

    DeliveryTrips.belongsTo(models.DeliveryExpeditionVendor, {
      foreignKey: "expedition_vendor_id",
      as: "expedition_vendor",
    });
  };

  return DeliveryTrips;
};
