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

subject: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"normal",


"high",


"urgent"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"in_progress",


"waiting_on_customer",


"resolved",


"closed"

        ],

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  support_tickets.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.support_tickets.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

    db.support_tickets.belongsTo(db.users, {
      as: 'assigned_admin_user',
      foreignKey: {
        name: 'assigned_admin_userId',
      },
      constraints: false,
    });




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

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



  return support_tickets;
};


