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

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"maintenance",


"room_service",


"concierge",


"it_support",


"other"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"normal",


"high",


"urgent"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"in_progress",


"resolved",


"cancelled"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

reported_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  service_requests.associate = (db) => {


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






























//end loop



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

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

    db.service_requests.belongsTo(db.guests, {
      as: 'reported_by',
      foreignKey: {
        name: 'reported_byId',
      },
      constraints: false,
    });

    db.service_requests.belongsTo(db.users, {
      as: 'assigned_to',
      foreignKey: {
        name: 'assigned_toId',
      },
      constraints: false,
    });



    db.service_requests.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.service_requests.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return service_requests;
};


