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,
      },

service_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"retrieval",


"coordination",


"administrative_support",


"concierge_assistance",


"custom_request",


"communication_support",


"premium_coordination"

        ],

      },

timing_preference: {
        type: DataTypes.ENUM,
      
      

        values: [

"asap",


"same_day",


"scheduled",


"flexible"

        ],

      },

scheduled_start_at: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end_at: {
        type: DataTypes.DATE,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"submitted",


"coordinator_assigned",


"in_progress",


"coordination_active",


"review_complete",


"successfully_closed",


"cancelled"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"standard",


"rush_priority",


"after_hours"

        ],

      },

estimated_total: {
        type: DataTypes.DECIMAL,
      
      

      },

final_total: {
        type: DataTypes.DECIMAL,
      
      

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

      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












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


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


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


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


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



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















//end loop



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

    db.service_requests.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

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

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

    db.service_requests.belongsTo(db.service_packages, {
      as: 'service_package',
      foreignKey: {
        name: 'service_packageId',
      },
      constraints: false,
    });




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

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



  return service_requests;
};


