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 bookings = sequelize.define(
    'bookings',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

booking_code: {
        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: [

"draft",


"pending_confirmation",


"confirmed",


"checked_in",


"checked_out",


"cancelled",


"no_show"

        ],

      },

source: {
        type: DataTypes.ENUM,
      
      

        values: [

"web",


"admin",


"phone",


"email",


"ota"

        ],

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

city_tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

deposit_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

deposit_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"none",


"requested",


"paid",


"refunded"

        ],

      },

special_requests: {
        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,
    },
  );

  bookings.associate = (db) => {

    db.bookings.belongsToMany(db.rooms, {
      as: 'reserved_rooms',
      foreignKey: {
        name: 'bookings_reserved_roomsId',
      },
      constraints: false,
      through: 'bookingsReserved_roomsRooms',
    });

    db.bookings.belongsToMany(db.rooms, {
      as: 'reserved_rooms_filter',
      foreignKey: {
        name: 'bookings_reserved_roomsId',
      },
      constraints: false,
      through: 'bookingsReserved_roomsRooms',
    });

    db.bookings.belongsToMany(db.booking_guests, {
      as: 'booking_guests',
      foreignKey: {
        name: 'bookings_booking_guestsId',
      },
      constraints: false,
      through: 'bookingsBooking_guestsBooking_guests',
    });

    db.bookings.belongsToMany(db.booking_guests, {
      as: 'booking_guests_filter',
      foreignKey: {
        name: 'bookings_booking_guestsId',
      },
      constraints: false,
      through: 'bookingsBooking_guestsBooking_guests',
    });

    db.bookings.belongsToMany(db.payments, {
      as: 'payments',
      foreignKey: {
        name: 'bookings_paymentsId',
      },
      constraints: false,
      through: 'bookingsPaymentsPayments',
    });

    db.bookings.belongsToMany(db.payments, {
      as: 'payments_filter',
      foreignKey: {
        name: 'bookings_paymentsId',
      },
      constraints: false,
      through: 'bookingsPaymentsPayments',
    });

    db.bookings.belongsToMany(db.messages, {
      as: 'messages',
      foreignKey: {
        name: 'bookings_messagesId',
      },
      constraints: false,
      through: 'bookingsMessagesMessages',
    });

    db.bookings.belongsToMany(db.messages, {
      as: 'messages_filter',
      foreignKey: {
        name: 'bookings_messagesId',
      },
      constraints: false,
      through: 'bookingsMessagesMessages',
    });


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



















    db.bookings.hasMany(db.booking_guests, {
      as: 'booking_guests_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });


    db.bookings.hasMany(db.payments, {
      as: 'payments_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });


    db.bookings.hasMany(db.invoices, {
      as: 'invoices_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });


    db.bookings.hasMany(db.messages, {
      as: 'messages_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });



    db.bookings.hasMany(db.booking_discounts, {
      as: 'booking_discounts_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });



    db.bookings.hasMany(db.service_requests, {
      as: 'service_requests_booking',
      foreignKey: {
          name: 'bookingId',
      },
      constraints: false,
    });





//end loop



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

    db.bookings.belongsTo(db.guests, {
      as: 'primary_guest',
      foreignKey: {
        name: 'primary_guestId',
      },
      constraints: false,
    });

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

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




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

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



  return bookings;
};


