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

reservation_code: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"quoted",


"confirmed",


"checked_in",


"checked_out",


"canceled",


"no_show"

        ],

      },

check_in_at: {
        type: DataTypes.DATE,
      
      

      },

check_out_at: {
        type: DataTypes.DATE,
      
      

      },

actual_check_in_at: {
        type: DataTypes.DATE,
      
      

      },

actual_check_out_at: {
        type: DataTypes.DATE,
      
      

      },

early_check_in: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

late_check_out: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

guest_count: {
        type: DataTypes.INTEGER,
      
      

      },

nightly_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

monthly_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

internal_notes: {
        type: DataTypes.TEXT,
      
      

      },

external_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  reservations.associate = (db) => {

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

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

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

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

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

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

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

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

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

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


/// 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.unit_availability_blocks, {
      as: 'unit_availability_blocks_reservation',
      foreignKey: {
          name: 'reservationId',
      },
      constraints: false,
    });







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


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


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




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




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


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





//end loop



    db.reservations.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

    db.reservations.belongsTo(db.booking_requests, {
      as: 'booking_request',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.properties, {
      as: 'property',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.units, {
      as: 'unit',
      foreignKey: {
        name: 'unitId',
      },
      constraints: false,
    });

    db.reservations.belongsTo(db.unit_types, {
      as: 'unit_type',
      foreignKey: {
        name: 'unit_typeId',
      },
      constraints: false,
    });




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

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



  return reservations;
};


