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

request_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"airport_pickup",


"housekeeping",


"maintenance",


"stocked_fridge",


"chauffeur",


"late_checkout",


"extension",


"custom_service"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"triaged",


"in_progress",


"scheduled",


"waiting_on_guest",


"completed",


"canceled"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"normal",


"high",


"urgent"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

details: {
        type: DataTypes.TEXT,
      
      

      },

estimated_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

actual_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

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

  service_requests.associate = (db) => {

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

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

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

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


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
























    db.service_requests.hasMany(db.documents, {
      as: 'documents_service_request',
      foreignKey: {
          name: 'service_requestId',
      },
      constraints: false,
    });




    db.service_requests.hasMany(db.activity_comments, {
      as: 'activity_comments_service_request',
      foreignKey: {
          name: 'service_requestId',
      },
      constraints: false,
    });






//end loop



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

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

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

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

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




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

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



  return service_requests;
};


