const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const reservations = sequelize.define(
    'reservations',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

reservation_number: {
        type: DataTypes.TEXT,
      
      

      },

check_in_at: {
        type: DataTypes.DATE,
      
      

      },

check_out_at: {
        type: DataTypes.DATE,
      
      

      },

adults: {
        type: DataTypes.INTEGER,
      
      

      },

children: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"confirmed",


"checked_in",


"checked_out",


"cancelled",


"no_show"

        ],

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

deposit_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

special_requests: {
        type: DataTypes.TEXT,
      
      

      },

internal_notes: {
        type: DataTypes.TEXT,
      
      

      },

cancelled_at: {
        type: DataTypes.DATE,
      
      

      },

cancellation_reason: {
        type: DataTypes.TEXT,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  reservations.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity




















    db.reservations.hasMany(db.reservation_guests, {
      as: 'reservation_guests_reservation',
      foreignKey: {
          name: 'reservationId',
      },
      constraints: false,
    });


    db.reservations.hasMany(db.stay_folios, {
      as: 'stay_folios_reservation',
      foreignKey: {
          name: 'reservationId',
      },
      constraints: false,
    });



    db.reservations.hasMany(db.payments, {
      as: 'payments_reservation',
      foreignKey: {
          name: 'reservationId',
      },
      constraints: false,
    });






    db.reservations.hasMany(db.notifications, {
      as: 'notifications_reservation',
      foreignKey: {
          name: 'reservationId',
      },
      constraints: false,
    });







//end loop



    db.reservations.belongsTo(db.hotels, {
      as: 'hotel',
      foreignKey: {
        name: 'hotelId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.guests, {
      as: 'guest',
      foreignKey: {
        name: 'guestId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.rooms, {
      as: 'room',
      foreignKey: {
        name: 'roomId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.room_types, {
      as: 'room_type',
      foreignKey: {
        name: 'room_typeId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.rate_plans, {
      as: 'rate_plan',
      foreignKey: {
        name: 'rate_planId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.sales_channels, {
      as: 'sales_channel',
      foreignKey: {
        name: 'sales_channelId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




    db.reservations.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.reservations.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return reservations;
};


